Several forms of ASP. net mvc passing parameters from a view to a controller, asp. netmvc
1. Pass the Array
$ (Function () {var value = ["C #", "JAVA", "PHP"]; $ ("input [type = 'click']"). click (function () {$. ajax ({url: "/Home/List", type: "Get", data: {valuelist: value}, traditional: true, // you must set this attribute, otherwise, the success: function (data) {alert ("Success") ;}}) ;}); public ActionResult List (List <string> valuelist) cannot be obtained in the controller) {return View ();}
Debugging results:
2. Pass a single Model
@ Using (Html. beginForm () {<div class = "form-group"> @ Html. labelFor (model => model. name, new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. name) @ Html. validationMessageFor (model => model. name) </div> <div class = "form-group"> @ Html. labelFor (model => model. price, new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. price) @ Html. validationMessageFor (model => model. price) </div> <div class = "form-group"> @ Html. labelFor (model => model. color, new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. color) @ Html. validationMessageFor (model => model. color) </div> <div class = "form-group"> <div class = "col-md-offset-2 col-md-10"> <input type = "submit" value = "submit" class = "btn-default"/> </div>}
Public class Products {public int Id {get; set;} [DisplayName ("Product Name")] [Required (ErrorMessage = "this item cannot be blank")] public string Name {get; set;} [DisplayName ("product Price")] [Required (ErrorMessage = "this item cannot be blank")] public string Price {get; set ;} [DisplayName ("product Color")] [Required (ErrorMessage = "this item cannot be blank")] public string Color {get; set ;}} public ActionResult Add (Products product) {return View ();}
Debugging results:
3. Pass Multiple Models
$ ("Input [type = 'submit ']"). click (function () {var promodes = []; promodes. push ({Id: "0", Name: "Mobile Phone", Color: "white", Price: "2499"}); promodes. push ({Id: "1", Name: "headset", Color: "black", Price: "268"}); promodes. push ({Id: "2", Name: "charger", Color: "yellow", Price: "99"}); $. ajax ({url: "/Home/List", type: "Post", data: JSON. stringify (promodes), // The array must be serialized contentType: "application/json", // set the value of contentType to "application/json ", the default value is "application/json" success: function (data) {alert ("Success ");}});});
public ActionResult List(List<Products> valuelist) { return View(); }
Debugging results:
The above is a small series of ASP. net mvc involves several forms of passing parameters from the view to the Controller. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!