【问题标题】:JS int array to MVC3 controllerJS int 数组到 MVC3 控制器
【发布时间】:2012-01-07 01:16:17
【问题描述】:

我在 JS 中有一个字符串数组。所有成员实际上都是数字。
我的控制器有一个 int[] 参数。

我从 jquery 发送它:

$.ajax({url: someUrl, data: {ids : JSON.stringify(id_array) }, ...)

我使用

收到它
public ActionResult MyAction(int[] ids) { ...

参数没有填满,我查过,Request.Form["ids"] 包含“[\"25\",\"26\"]"”,JSON 数组的字符串表示。 有没有办法自动无需进行大量的 int 解析?

【问题讨论】:

    标签: jquery ajax arrays json asp.net-mvc-3


    【解决方案1】:

    您是否尝试过不对数组进行字符串化,而是让 mvc 的默认模型绑定发挥作用

    $.ajax({url: someUrl, data: {ids : id_array }, ...)
    

    我很确定 .net MVC 会看到该数组并看到它是一个整数数组并将其正确映射到您的整数数组

    【讨论】:

    • 是的,Request.Form["ids[]"] 的值将是“25,26”,int[] 仍然没有绑定。
    • @TDaver 您需要将 .ajax "traditional" 属性设置为 true,以便浅数组序列化也可以根据 api.jquery.com/jQuery.param 正常工作
    • 我所缺少的只是传统的旗帜。谢谢
    • @Keith.Abramo 请在您的回答中包含对传统属性的引用。
    【解决方案2】:

    对于 MVC3,您需要将 jQuery 配置为使用传统的“浅”序列化。请参阅 here

    客户端 AJAX 请求:

    jQuery.ajaxSettings.traditional = true; //enable "shallow" serialisation globally
    $.get("someUrl", {ids : id_array });
    

    动作方法:

    public ActionResult MyAction(string[] ids) { ... }
    

    【讨论】:

      【解决方案3】:

      您可以通过两种不同的方式来做到这一点

      1.使用 $.ajax 将数据发布到您的操作中

      $.ajax({
          url: '/myController/myAction',
          dataType: 'json',
          type: 'POST',
          contentType: 'application/json; charset=utf-8',
          traditional: true,
          data: $.toJSON(json),
          success: function (data, textStatus, jqXHR) {
          ...
          }
      });
      

      你的动作应该是这样的

      public class myController
      {
          [HttpPost]
          public ActionResult myAction(List<int> json)
          {
              ....
          }
      }
      

      2。使用 $.post 发布您的数据

      $.post(
          '/myController/myAction',
          { json: $.toJSON(products) },
          function (data) {
          ...
      });
      

      在您的操作中,您应该反序列化 JSON 字符串

      public class myController
          {
          public ActionResult myAction(string json)
          {
              JavaScriptSerializer serializer = new JavaScriptSerializer();
              List<int> list = serializer.Deserialize<List<int>>(json);
          }
      }
      

      您需要下载 jquery.json-2.2.min.js 并将其包含到您的代码中。

      http://code.google.com/p/jquery-json/downloads/detail?name=jquery.json-2.2.min.js

      【讨论】:

        【解决方案4】:

        你需要使用

        $.ajax({
            url: someUrl, 
            data: id_array.map( function(item){ return {name:'ids',value:item}; })
        })
        

        那是因为为了在后端接收列表,需要以ids=3&amp;ids=5&amp;ids=1&amp;ids=7的形式发送数据


        对于不支持.map的IE版本使用

        function arrayToParamObject(name,array){
            var obj = [];
            for (var i = 0, len = array.length; i< len; i++){
                obj.push( {name: name, value: array[i] } );
            }
            return obj;
        }
        

        并使用它

        $.ajax({
            url: someUrl, 
            data: arrayToParamObject('ids', id_array)
        })
        

        【讨论】:

          猜你喜欢
          • 2017-10-21
          • 1970-01-01
          • 1970-01-01
          • 2020-03-04
          • 2012-10-05
          • 1970-01-01
          • 2020-10-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多