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 "https://stackoverflow.com/questions/24027245/mvc-any-version-pass-nested-complex-json-object-to-controllers", which also has not been answered.
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;
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) {
}
});
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");
}
This is what I've observed:
- With
traditional:trueI receive thearrayDatawhich is a simple array in the/Home/Savecontroller but thearrayOfarrayDatais empty - Without
traditional:trueI receivearrayDataasnullbutarrayOfarrayDatais received as an array of 5 elements but the elements are not the sub-array information you would expect but its empty
PS: An array of array is only an example, no NESTED complex type seem to work. Or is there any way around this?