Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report