3

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:true I receive the arrayData which is a simple array in the /Home/Save controller but the arrayOfarrayData is empty
  • Without traditional:true I receive arrayData as null but arrayOfarrayData is 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?

1 Answer 1

4

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) {
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

This is correct, but as a sidenote you may need a polyfill for JSON.strigify on older browsers (github.com/douglascrockford/JSON-js).
Also as a further sidenote, "traditional" is irrelevant once you're stringifying the data - it will have absolutely no impact on how it's processed, but leaving it out is probably less misleading because having it there implies it's doing something.
@user3398887 I am sorry, but using stringify alone did not fix it but thank you, I had to use stringify and contentType and then it worked, could you please modify your answer to include contentType so that I can mark it to be correct answer :)
@fyjham, thank you for the suggestions and yes, you were correct about traditional I even tried that out :)
Glad to know that works for you. Answer has been modified, you can mark it to be correct one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.