如何JSON对象传递给控制器​​的方法在ASP.NET MVC 4 [英] How to pass JSON object to controller method in ASP.NET MVC 4

查看:262
本文介绍了如何JSON对象传递给控制器​​的方法在ASP.NET MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表格并想的表的内容传递到一个控制器的方法。我使用jQuery来创建一个数组,然后把它们变成一个JSON对象。

  VAR array_MACAddress = [];
                           变量$ MACADDRESS = $(MACADDRESS。);                           对于(VAR I = 0,LEN = $ MacAddress.length; I< LEN,我++){
                               变量$ MAC = $($ MACADDRESS [I]);
                               的console.log($ MAC.text()); ////只用于测试!
                               array_MACAddress.push({
                                   键:$ MAC.data(钥匙),
                                   价值:$ MAC.text()
                               });
                           };

(在code上面创建一个名为 MACADDRESS 列的数组),然后我用下面的code将它们变成JSON对象

  VAR json_MACAddress = json.stringify(array_MACAddress);

我如何通过JSON对象到控制器的方法?

 公众的ActionResult Activati​​onManagement(字符串jsonData)


解决方案

为了使AJAX请求,你可以这样做:

  $(#someDomElement)。点击(函数(){
   $阿贾克斯({
     网址:@ Url.Content(〜/ YourController / Activati​​onManagement),
     数据:array_MACAddress,
     输入:POST,
     的contentType:JSON
   });
});

您将需要指定将匹配您的Ajax模型包含您的MAC地址视图模型:

 公共类MACADDRESS
{
   公共字符串键{获得;组; }
   公共字符串值{获得;组; }
}

由于您有效地有你需要如下定义控制器操作这些对象的列表:

  [HttpPost]
公众的ActionResult Activati​​onManagement(列表<&MACADDRESS GT; macAddresses)
{
   //你的逻辑在这里...
}

和MVC模型绑定会自动出来,因为contentType中在你的AJAX调用指定,你发送的数据是JSON类型映射到模型。

我给你的建议是章安提琴手( http://fiddler2.com/ ),这将帮助您分析每个请求做出这样你可以清楚地看到是否有问题,以及如何解决它。

I have an HTML table and would like to pass the content of the table into a controller method. I use JQuery to create an array then turn them into a JSON object.

                           var array_MACAddress = [];
                           var $MacAddress = $(".macaddress");

                           for (var i = 0, len = $MacAddress.length; i < len; i++) {
                               var $MAC = $($MacAddress[i]);
                               console.log($MAC.text()); ////for testing only!!!
                               array_MACAddress.push({
                                   key: $MAC.data("key"),
                                   value: $MAC.text()
                               });
                           }; 

(the code above it to create an array of a column called "MACAddress"), then I use the code below to turn them into JSON object

var json_MACAddress = json.stringify(array_MACAddress);

How do I pass the JSON object into the controller method?

public ActionResult ActivationManagement(String jsonData)

解决方案

To make the AJAX request you can do this:

$("#someDomElement").click(function() {
   $.ajax({
     url: "@Url.Content("~/YourController/ActivationManagement")",
     data: array_MACAddress,
     type: 'POST',
     contentType: 'json'
   });
});

You will need to specify a view model that will match your ajax model that contains your mac addresses:

public class MacAddress
{
   public string Key { get; set; }
   public string Value { get; set; }
}

Since you effectively have a list of these objects you need the define the controller action as follows:

[HttpPost]
public ActionResult ActivationManagement(List<MacAddress> macAddresses)
{
   // Your logic here...
}

and MVC model binder will automatically map it out to your model since in contentType in you AJAX call you specified that the data you're sending is of JSON type.

My advice to you is to insta Fiddler (http://fiddler2.com/) which will help you analyze each request made so that you can clearly see if there is a problem and how to resolve it.

这篇关于如何JSON对象传递给控制器​​的方法在ASP.NET MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆