Get the App
SLTechnology News&Howtos  ›  Development  › 

There are several forms of .NET MVC passing parameters from view to controller.

Shulou Source: shulou.com Published: 2022-06-03 13:25:56 10月07日 Update

This article shares with you several forms of .NET MVC that pass parameters from views to controllers. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Transitive array

$(function () {var value = ["C #", "JAVA", "PHP"] Click (function () {$.ajax ({url: "/ Home/List", type: "Get", data: {valuelist: value}, traditional: true, / / this attribute must be set Otherwise, the value success: function (data) {alert ("Success") cannot be obtained in the controller. }); public ActionResult List (List valuelist) {return View ();}

Debugging effect:

two。 Pass a single Model

Using (Html.BeginForm ()) {@ Html.LabelFor (model = > model.Name, new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Name) @ Html.ValidationMessageFor (model = > model.Name)

@ Html.LabelFor (model = > model.Price, new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Price) @ Html.ValidationMessageFor (model = > model.Price)

@ Html.LabelFor (model = > model.Color, new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Color) @ Html.ValidationMessageFor (model = > model.Color)

} public class Products {public int Id {get; set;} [DisplayName ("Product name")] [Required (ErrorMessage = "this item cannot be empty")] public string Name {get; set;} [DisplayName ("Product Price")] [Required (ErrorMessage = "this item cannot be empty")] public string Price {get; set } [DisplayName ("Product Color")] [Required (ErrorMessage = "this item cannot be empty")] public string Color {get; set;}} public ActionResult Add (Products product) {return View ();}

Debugging effect:

3. Pass multiple Model

Click (function () {var promodes = []; promodes.push ({Id: "0", Name: "Mobile", Color: "White", Price: "2499"}); promodes.push ({Id: "1", Name: "headset", Color: "Black", Price: "2499"}) Promodes.push ({Id: "2", Name: "charger", Color: "yellow", Price: "99"}) $.ajax ({url: "/ Home/List", type: "Post", data: JSON.stringify (promodes), / / must serialize the array contentType: "application/json", / / set the value of contentType to "application/json"

Debugging effect:

Thank you for reading! This is the end of this article on "there are several forms for .NET MVC to transfer parameters from view to controller". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Controller control product effect form view content array more article good practical price charger single name multiple attributes sequence mobile phone Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info Microsoft Huawei MariaDB