Several forms of ASP. net mvc passing parameters from a view to a controller, asp. netmvc

Source: Internet
Author: User

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!

Related Article

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.