2 ways MVC uses jquery to pass model to a controller from a view

Source: Internet
Author: User

http://blog.csdn.net/make1828/article/details/29846003

Using System.componentmodel.dataannotations;namespace mvcapplication1.models{public    class Movie    {        public  int Id {get; set;}        [Required (errormessage = "required")]        public string Title {get; set;}}    }

In HomeController, an action is used to display a strongly typed view, one to receive the model passed from the view.

Using system.web.mvc;using mvcapplication1.models;namespace mvcapplication1.controllers{public    class Homecontroller:controller    {public        actionresult Index ()        {            return View (New Movie ());        }        Public ActionResult Addmovie (movie movie)        {            return Json (new {msg = "OK"});        }    }

Method One: Through the Serialize () method of jquery

@model mvcapplication1.models.movie@{viewbag.title = "Index"; Layout = "~/views/shared/_layout.cshtml";} 

Method two: Using JQuery's Json.stringify () method to convert an anonymous object to JSON

$ (function () {            $ (' #addMovie '). Click (function (e) {                e.preventdefault ();                if ($ (' #addForm '). Valid ()) {                    addmovie ();                }            );        });        function Addmovie () {            var movie = {                "Title": $ (' #Title '). Val ()            };            $.ajax ({                URL: ' @Url. Action ("Addmovie", "Home") ',                data:JSON.stringify (movie),                type: ' POST ',                ContentType: ' Application/json;charset=utf-8 ',                success:function (data) {                    if (data.msg = = ' OK ') {                        alert (' submit success ');}}            );        }

MVC uses jquery to pass Model 2 ways from view to controller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.