This article mainly introduces the ASP. NET MVC from the view to the controller of several forms, very good, with reference value, the need for friends can refer to the following
1. Passing arrays
$ (function () { var value = ["C #", "JAVA", "PHP"]; $ ("input[type= ' button ']"). Click (function () { $.ajax ( { URL: "/home/list", Type: "Get", data: { Valuelist:value}, traditional:true,//Must be set for this property, otherwise the controller will not get the value success:function (data) { alert ("Success") ; } }); }); }); Public ActionResult List (list<string> valuelist) { return View (); }
Debug Effect:
2. Passing a single model
@using (Html.BeginForm ()) {<p class= "Form-group" > @Html. labelfor (model = model. Name, new {@class = "Control-label col-md-2"}) <p class= "col-md-10" > @Html. editorfor (model = model . Name) @Html. validationmessagefor (model = model. Name) </p> </p> <p class= "Form-group" > @Html. labelfor (model = model. Price, new {@class = "Control-label col-md-2"}) <p class= "col-md-10" > @Html. editorfor (model = mode L.price) @Html. validationmessagefor (model = model. Price) </p> </p> <p class= "Form-group" > @Html. labelfor (model = model. Color, new {@class = "Control-label col-md-2"}) <p class= "col-md-10" > @Html. editorfor (model = mode L.color) @Html. validationmessagefor (model = model. Color) </p> </p> <p class= "Form-group" > <p class= "col-md-offset-2 col-md-10" > <input type= "Submit "value=" Submission "class=" btn btn-default "/> </p> </p>"
public class Products {public int Id {get; set;} [DisplayName ("Product name")] [Required (errormessage = "This entry cannot be empty")] public string Name {get; set;} [DisplayName ("Product price")] [Required (errormessage = "This entry cannot be empty")] public string Price {get; set;} [DisplayName ("Product Color")] [Required (errormessage = "This entry cannot be empty")] public string Color {get; set;} } Public ActionResult ADD (products product) { return View (); }
Debug Effect:
3. Passing Multiple model
$ ("input[type= ' Submit ']"). Click (function () { var promodes = []; Promodes.push ({Id: "0", Name: "Phone", Color: "White", Price: "2499"}); Promodes.push ({Id: "1", Name: "Headset", Color: "Black", Price: "268"}); Promodes.push ({Id: "2", Name: "Charger", Color: "Yellow", Price: ","}); $.ajax ( { URL: "/home/list", Type: "Post", data:JSON.stringify (promodes),//Must be serialized on an array ContentType: "Application/json",//Set the value of ContentType to "Application/json", Default to "Application/json" success: function (data) { alert ("Success");});}) ;
Public ActionResult List (list<products> valuelist) { return View (); }
Debug Effect: