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