传递选择多选择值到控制器的动作 [英] Passing chosen multi select values to controller action

查看:133
本文介绍了传递选择多选择值到控制器的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的选择多选择列表中选择的值发送到我的控制器中的操作。我已经验证VAL()不告诉我,如[33,175]中选择值的数组,当我打印到控制台,但行动的说法总是空。我试图改变参数类型为对象,并验证它不为空,但我无法解析的值。有什么建议么?请和谢谢!

Ajax调用:

  $(选择 - 选择)。在(变,功能(事件,则params){
    的console.log($(选择 - 选择)VAL());

    $阿贾克斯({
        网址:@ Url.Action(BDOReferralSourcesTableHTML,ReferralNetwork),
        键入:GET,
        数据类型:JSON,
        缓存:假的,
        数据:{bdoIds:(选择 - 选择)。$ VAL()},
        成功:函数(响应){
            如果(response.length大于0){
                警报(响应);
            }
            其他 {
                警报(响应长度为零);
            }
        }
    });
});
 

控制器动作:

 公众的ActionResult BDOReferralSourcesTableHTML(字符串[] bdoIds)
{
    返回内容(< B>测试< / B>中,text / html的);
}
 

解决方案

您必须设置传统 AJAX的财产

  $。阿贾克斯({
        网址:@ Url.Action(BDOReferralSourcesTableHTML,ReferralNetwork),
        键入:GET,
        数据类型:JSON,
        缓存:假的,
        传统:真正的,
        数据:{bdoIds:(选择 - 选择)。$ VAL()},
        成功:函数(响应){
            如果(response.length大于0){
                警报(响应);
            }
            其他 {
                警报(响应长度为零);
            }
        }
    });
 

您也可以全局设置,所以没有必要在每一个Ajax调用指定它:

  $。ajaxSetup({
    传统:真
   });
 

更新:

下面就是为什么你需要设置的原因 传统:真正的

  
    

的jQuery 1.4 增加了对嵌套参数序列化支持在 jQuery.param ,利用PHP推广的办法,和Ruby on Rails的支持。例如, {FOO:酒吧,巴兹]} 将被序列化为富[] =酒吧和放大器;富[] =巴兹

         

的jQuery 1.3 {FOO:酒吧,巴兹]} 连载作为富=酒吧和放大器;富=巴兹。但是,有没有办法连接codeA使用这种方法单元素数组。如果需要旧的行为,你可以把它背在全球范围内所通过的 jQuery.ajaxSettings.traditional 或在设置传统的Ajax设置(逐案通过传统标志的基础上)。

  

详细信息可以看出 的jQuery 1.4发布 - 全面发行说明 阿贾克斯部分。

您还可以看到的解释<一个href="http://dovetailsoftware.com/clarify/kmiller/2010/02/24/jquery-1-4-breaks-asp-net-mvc-actions-with-array-parameters/"相对=nofollow> 在这里。 jQuery的1.4打破了数组参数ASP.Net MVC的行动

另请参见: jQuery的1.4休息ASP.NET MVC参数发布

I'm trying to send the selected values of a chosen multi select list to an Action in my Controller. I've verified val() does show me an array of selected values like ["33","175"] when I print out to the console but the Action's argument is always null. I tried changing the argument type to object and verified it's not null but I can't parse the values. Any suggestions? Please and thanks!

Ajax call:

$(".chosen-select").on("change", function (event, params) {
    console.log($(".chosen-select").val());

    $.ajax({
        url: '@Url.Action("BDOReferralSourcesTableHTML","ReferralNetwork")',
        type: 'GET',
        dataType: 'json',
        cache: false,
        data: { bdoIds: $(".chosen-select").val() },
        success: function (response) {
            if (response.length > 0) {
                alert(response);
            }
            else {
                alert("response length zero");
            }
        }
    });
});

Controller Action:

public ActionResult BDOReferralSourcesTableHTML(string[] bdoIds)
{
    return Content("<b>test</b>", "text/html");    
}

解决方案

you have to set traditional property of ajax to true:

$.ajax({
        url: '@Url.Action("BDOReferralSourcesTableHTML","ReferralNetwork")',
        type: 'GET',
        dataType: 'json',
        cache: false,
        traditional:true,
        data: { bdoIds: $(".chosen-select").val() },
        success: function (response) {
            if (response.length > 0) {
                alert(response);
            }
            else {
                alert("response length zero");
            }
        }
    });

you can also set it globally so there is no need to specify it in every ajax call:

$.ajaxSetup({
    traditional:true
   });

UPDATE:

Following is the reason why you need to set traditional:true:

jQuery 1.4 adds support for nested param serialization in jQuery.param, using the approach popularized by PHP, and supported by Ruby on Rails. For instance, {foo: ["bar", "baz"]} will be serialized as "foo[]=bar&foo[]=baz".

In jQuery 1.3, {foo: ["bar", "baz"]} was serialized as "foo=bar&foo=baz". However, there was no way to encode a single-element Array using this approach. If you need the old behavior, you can turn it back on by setting the traditional Ajax setting (globally via jQuery.ajaxSettings.traditional or on a case-by-case basis via the traditional flag).

Details can be seen jQuery 1.4 Released – Full Release Notes in Ajax section.

You can also see the explanation here. jQuery 1.4 breaks ASP.Net MVC actions with array parameters

Also see: jQuery 1.4 breaks ASP.NET MVC parameter posting

这篇关于传递选择多选择值到控制器的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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