views:

115

answers:

1

I am trying to pass a nested array to an asp.net mvc controller like this:

var subgroups = [];
subgroups [0] = [["A", "Y"],
                 ["B", "Y"],
                 ["C", "Y"]];
subgroups [1] = [["D", "Z"],
                 ["E", "Z"],
                 ["F", "Z"]];

$.ajax({ url: "<%= Url.Content("~/Controller/Action") %>", data: { subgroups: subgroups },
         dataType: "json", context: document.body, success: function(data) { ... } );

The resulting controller:

public ContentResult Action(List<string[][]> subgroups) {
    ...
}

The problem is the resulting subgroups variable in the controller looks like this:

subgroups[0][0] == null;
subgroups[0][1] == null;
subgroups[0][2] == null;
subgroups[1][0] == null;
subgroups[1][1] == null;
subgroups[1][2] == null;

The values from the deeply nested array don't get passed. Firebug reports the parameters of my ajax request are:

subgroups[0][0][] A
subgroups[0][0][] Y
subgroups[0][1][] B
subgroups[0][1][] Y
subgroups[0][2][] C
subgroups[0][2][] Y
subgroups[1][0][] D
subgroups[1][0][] Z
subgroups[1][1][] E
subgroups[1][1][] Z
subgroups[1][2][] F
subgroups[1][2][] Z

It looks like the cause is that jQuery isn't filling in these array indexes because I can fix the problem is I format the data as:

subgroups [0] = [{ 0: "A", 1: "Y"}, ...];

Why does jQuery put the indexes for the outer array, but not the inner one? Please tell me there is a better workaround for this than to manually specify numeric keys?

+1  A: 

in file "code.jquery.com/jquery-1.4.2.js" in line 5290:

buildParams( prefix + "[" + ( typeof v === "object" jQuery.isArray(v) ? i : "" ) + "]", v );

I change for this code:

buildParams(prefix + (typeof v === "object" jQuery.isArray(v) ? "[" + i + "]" : ""), v);

and the result was good. I could not test on many more cases.

I create a ticket in jQuery.com to see if this is a bug: Ticket #6900 (new bug)

but in:

public ContentResult Action(List<string[][]> subgroups) {

for this modification, you have to use this code:

public ContentResult Action(List<string[]> subgroups) {
andres descalzo
+1 For being one of the bravest jquery men I have come across :)
Jimbo
Your solution didn't quite work for me, but it helped me to find a solution by changing line 5419 to:`buildParams( prefix + "[" + i + "]", v );`Thank you for opening a bug report and leading me to a solution. I'm not sure if this is a bug or intended jQuery behavior, but for people running ASP.net MVC this behavior seem more useful.
Code Commander
yes, I copied the wrong line number, the right is 5419. Thanks
andres descalzo