There is a Model like this:
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 strong view, and a Model is used 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 1: Use the serialize () method of jQuery
@ Model MvcApplication1.Models. Movie @ {ViewBag. Title = "Index"; Layout = "~ /Views/Shared/_ Layout. cshtml ";}< h2> Index
Method 2: convert an anonymous object to JSON using the json. stringify () method of jQuery.
$ (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 ('submitted successfully ');}}});}