【问题标题】:Jquery Multiselect to MVC4 ActionJquery Multiselect 到 MVC4 动作
【发布时间】:2013-09-24 09:07:17
【问题描述】:

我正在尝试接收我的多选框的选定值。通过 Ajax 调用。

下面是我的测试动作

public ActionResult MultiSelect(String[] test)
{
    String[] arrayornot = test; //null being recieved. or the string if hardcoded
}

Jquery

    alert($('#county').val()); // London, Brim
    $.ajax({
        url: '@Url.Action("MultiSelect", "APITest")',
        type: 'GET',
        cache: false,
        data: { test: $('#county').val()},
        success: function (result) {
            $('#myDiv').html(result);
        }
    });

如果我将它硬编码为一个字符串,它就可以正常工作。使用String[]String 端点。如果它以逗号分隔的字符串传递,我可以在服务器端对其进行排序。或者字符串数组更好。

【问题讨论】:

  • 有了将数组转换为字符串的想法,在 Jquery 中以逗号分隔,它实际上只是 $('#county').val().toString() 。仍然不确定如何将其作为数组传递。
  • 您是否尝试过删除数据的对象表示法?尝试解析参数时,它不会寻找具有名为 test 的属性的对象 - 它将寻找一个字符串数组。
  • 尝试将选中的项目添加到 JS 数组列表中,然后将其作为 JSON 字符串传递
  • @anthr 当我打印它时,我得到London, Brim 所以我不确定它甚至有什么对象表示法。或者如何将其映射到字符串数组。
  • @Murali 谢谢,但我更喜欢将其识别为数组的原因是我不必更改大量代码来接受字符串。我宁愿不必在服务器端反序列化它。无论哪种方式,它似乎都类似于发送普通的逗号分隔字符串。除非 JSON 字符串会映射到字符串数组?

标签: jquery asp.net-mvc asp.net-mvc-4 multi-select


【解决方案1】:

错误是 $Ajax 配置设置方法传统:true 那么你的问题就解决了。

var selectedItems=$('#county').val();

$.ajax({

    url: '@Url.Action("MultiSelect", "APITest")',

    type: 'POST',

    cache: false,

    traditional: true, 

    data: { test: JSON.stringify(selectedItems)},

    success: function (result) {
        $('#myDiv').html(result);
    }

});

【讨论】:

    【解决方案2】:

    我遇到了和你一样的问题。我在以下链接中找到了答案。

    http://dovetailsoftware.com/clarify/kmiller/2010/02/24/jquery-1-4-breaks-asp-net-mvc-actions-with-array-parameters

    基本上你需要做的是添加以下行,值将作为数组传递。

    jQuery.ajaxSettings.traditional = true;

    【讨论】:

      【解决方案3】:

      我会使用 javascript 数组并转换成 JSON 字符串

      var selectedItems=$('#county').val(); // returns the array of selected items
      

      然后使用JSON.stringify方法

      $.ajax({
              url: '@Url.Action("MultiSelect", "APITest")',
              type: 'GET',
              cache: false,
              data: { test: JSON.stringify(selectedItems)},
              success: function (result) {
                  $('#myDiv').html(result);
              }
          });
      

      JSON.Stringify 在 IE 7 中不可用。请使用JSON2.js

      希望对您有所帮助!

      【讨论】:

      • 我会试一试,但我必须问,SelectedItems 和 SelectedArray 有什么区别。我认为这些项目是一个数组:S 也许这就是我出错的地方。
      • @Doomsknight,太完美了:) 我改变了答案。拆分也返回数组:)
      • 我只是在数组中得到一个字符串值 = "[\"London\",\"Brim\"]" 的元素(与您建议的先前代码相同)
      • 还是一样,我的猜测是我缺少一些标志,比如说传入的 JSON。
      【解决方案4】:

      而不是在方法参数中使用string[](字符串数组)。使用string 参数。并将这个逗号分隔的字符串转换为服务器端的数组。

      使用以下代码,

      服务器端,

          public ActionResult MultiSelect(string test)
          {
              return View();
          }
      

      JQuery 代码,

      $.ajax({
                      url: '@Url.Action("MultiSelect", "OrderCreation")',
                      type: 'GET',
                      cache: false,
                      data: { test: $('#county').val().toString() },
                      success: function (result) {
                          $('#myDiv').html(result);
                      }
                  });
      

      【讨论】:

        猜你喜欢
        • 2010-11-06
        • 1970-01-01
        • 2019-09-13
        • 2013-06-08
        • 1970-01-01
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多