Passing chosen multi select values to controller action

感情迁移 提交于 2019-12-04 06:58:22

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!