Submit array param with jQuery ajax/load
Asked Answered
M

4

14

public ActionResult DoSomething(string[] arr, bool someBool, int someInt) { }

trying to call the above method from jQuery:

var test = [];
test.push('dog');
test.push('cat');

$container.load('MyController/DoSomething',
                { 'arr[]': test, 'someBool': true, 'someInt': 1 },
                function(response, status, xhr) {
                    // ...
                });

the array paramater is null, other params are fine. What am I doing wrong?

Chrome developer tools shows form data being submitted as

arr%5B%5D%5B%5D:dog
arr%5B%5D%5B%5D:cat
someBool:true
someInt:1

not sure whats going on there but doesn't look right to me

Madaih answered 15/10, 2010 at 12:57 Comment(0)
Y
29

If you are using jquery 1.4 you might need to set the traditional parameter to true in order to be compatible with the default model binder format in ASP.NET MVC:

var test = [];
test.push('dog');
test.push('cat');

$.ajax({
    url: 'MyController/DoSomething',
    type: 'GET',
    traditional: true,
    data: { arr: test, someBool: true, someInt: 1 },
    success: function(result) {
        $container.html(result);
    }
});

or if you prefer the .load() method:

var data = { arr: test, someBool: true, someInt: 1 };
$container.load('MyController/DoSomething', $.param(data, true), 
    function(response, status, xhr) {
    // ...
});
Yirinec answered 15/10, 2010 at 13:19 Comment(4)
yep that's it. I've actually come across this problem before but had forgotten until this answer jogged my memory.Madaih
Thanks for the example using 'load' instead of 'ajax'.Applejack
Same issue happens with Java Struts2 and this fix is also good for Struts2Ngo
Thanks especially for these three lines: var test = []; test.push('dog'); test.push('cat'); I don't know what I was doing wrong, but until I re-built my array using ".push", nothing worked for me.Insufficiency
B
1

Just remove []

{ 'arr': test, 'someBool': true, 'someInt': 1 },

Posted values (checking with Firebug).

arr[]       dog
arr[]       cat
someBool    true
someInt     1
Bashee answered 15/10, 2010 at 13:12 Comment(1)
removing the brackets doesn't make it work, I already tried without. Though note the jQuery documentation gives the example with brackets, so I presume thats the correct way: $("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );Madaih
G
0

can you see if this problem is similar to yours:

Passing an nested arrays to asp.net mvc using jQuery's $.ajax

Geyser answered 15/10, 2010 at 13:14 Comment(0)
T
0

Even i was facing error, in passing array from HTML page to aspx page.

my requirement was to load the aspx page in a DIV tag of the html page. on the page load i need to pass these JS array values to aspx page load.

i used below method.

$('#<divTagID>').load("Targetpage.aspx",{"Arr":JSArrValues});

In aspx page load event i can access this values as:

string results = Response["Arr[]"];

Thanks to JQuery API documentation enter link description here and stackoverflow

Truett answered 11/7, 2014 at 5:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.