传递嵌套复杂的JSON到MVC控制器 [英] Passing NESTED Complex json to MVC Controllers
问题描述
注意:这可能是促使一些,以纪念这是一个已经回答的问题,但它不是,我一直在寻找的答案相当长一段时间。这是<一个修改后的版本href="http://stackoverflow.com/questions/24027245/mvc-any-version-pass-nested-complex-json-object-to-controllers">MVC (所有版本) - 传递复杂的嵌套JSON对象控制器的,这也没有得到回答。
NOTE: It might be urging for some to mark this as an already answered question BUT it is not, I have been searching for an answer for quite a while. This is a modified version of "MVC (Any Version) - Pass Nested Complex JSON object to Controllers", which also has not been answered.
问题:是否有可能通过后jQuery的Ajax调用的JSON数据到一个MVC控制器,你逝去的数据是一个复杂类型更复杂的类型呢?实施例数组的数组
Question: Is it possible to post a JSON data through jQuery ajax call to a MVC Controller where the data you are passing is a complex type with more complex types in it? Example an array of arrays.
var arrayOfarrays = [];
var simpleArray = [];
simpleArray[simpleArray.length] = simpleArray.length + 1;
simpleArray[simpleArray.length] = simpleArray.length + 1;
simpleArray[simpleArray.length] = simpleArray.length + 1;
simpleArray[simpleArray.length] = simpleArray.length + 1;
simpleArray[simpleArray.length] = simpleArray.length + 1;
arrayOfarrays[arrayOfarrays.length] = simpleArray;
arrayOfarrays[arrayOfarrays.length] = simpleArray;
arrayOfarrays[arrayOfarrays.length] = simpleArray;
arrayOfarrays[arrayOfarrays.length] = simpleArray;
arrayOfarrays[arrayOfarrays.length] = simpleArray;
以上是我的数据。正如你看到的,一个 simpleArray
是一个单纯的阵列和 arrayOfarrays
是一个数组的数组这是一个方式嵌套的复杂类型
The above is my data. As you see, a simpleArray
is a mere array and the arrayOfarrays
is an array of arrays which is in a way a nested complex type
$.ajax({
url: '/Home/Save',
data: {arrayData:simpleArray, arrayOfarrayData:arrayOfarrays},
type: 'POST',
dataType: 'json',
traditional:true,
cache: false,
success: function (result) {
}
});
以上片段是我的jQuery Ajax调用控制器 /主页/保存
这里下面是控制器本身。请注意,我试图使用和不使用传统:真正的
选项
The above snippet is my jQuery ajax call to the controller /Home/Save
and here below is the controller itself. Note that I have tried with and without the traditional:true
option.
[HttpPost]
public JsonResult Save(int[] arrayData, int[][] arrayOfarrayData)
{
return Json("received");
}
这是我所观察到的:
- 使用
传统:真正的
我收到arrayData code>这是一个简单的数组的
/主页/保存
控制器,但arrayOfarrayData code>是空
- 没有
传统:真正的
我接受arrayData code>为
空
但arrayOfarrayData code>被接收为5个元素的数组,但该元素不是子阵列信息,你会期望,但其空
- With
traditional:true
I receive thearrayData
which is a simple array in the/Home/Save
controller but thearrayOfarrayData
is empty - Without
traditional:true
I receivearrayData
asnull
butarrayOfarrayData
is received as an array of 5 elements but the elements are not the sub-array information you would expect but its empty
<强> PS:强>阵列的阵列仅仅是一个例子,不NESTED复杂类型似乎工作。或者有没有解决这个办法吗?
PS: An array of array is only an example, no NESTED complex type seem to work. Or is there any way around this?
推荐答案
我想你需要字符串化您的数据。
I think you need to stringified your data
var dataToSend={
'arrayData':simpleArray,
'arrayOfarrayData':arrayOfarrays
};
$.ajax({
url: '/Home/Save',
data: JSON.stringify(dataToSend),
type: 'POST',
contentType: 'application/json',
dataType: 'json',
traditional:true,
cache: false,
success: function (result) {
}
});
这篇关于传递嵌套复杂的JSON到MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!