传递嵌套数组来ASP.NET MVC使用jQuery的$。阿贾克斯 [英] Passing nested arrays to asp.net mvc using jQuery's $.ajax

查看:99
本文介绍了传递嵌套数组来ASP.NET MVC使用jQuery的$。阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个嵌套数组传递给这样一个asp.net mvc的控制器:

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;

从深度嵌套数组中的值没有得到通过。 Firebug的报道我的Ajax请求的参数是:

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

它看起来像的原因是jQuery是不是在这些数组索引填充,因为我可以解决这个问题是我格式化数据:

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"}, ...];

为什么的jQuery置于外侧阵列中的索引,而不是内一个?请告诉我有这个一个更好的解决办法,而不是手动指定数字键?

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?

推荐答案

在文件code.jquery.com / jQuery的-1.4.2.js看齐5290:

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

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

我更改此code:

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.

我创建jQuery.com一票,看看这是一个错误:
票务#6900(新的bug)

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

但在:

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

有关此修改,你必须使用此code:

for this modification, you have to use this code:

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

这篇关于传递嵌套数组来ASP.NET MVC使用jQuery的$。阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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