I'm trying to pass an array from a jQuery function to my controller. The array contains content and the id of the div holding that content.
When I check the objects which are being sent via the AJAX post in Firebug the correct values are there but after placing a breakpoint on my controller the received value is an empty List or Array or whatever type I try to set it to. I'm fairly new to using JSON to pass data to my controllers so would appreciate some help in where I'm going wrong.
jQuery function called on "submit" click. The array is globally declared in my script and is added to each time a new area is filled with content.
function postContent() {
$.ajax({
type: "POST",
datatype: 'json',
url: "/Admin/getContentArray",
data: JSON.stringify(contentArray),
contentType: 'application/json; charset=utf-8',
success: function (result) {
alert(result.Result);
}
});
}
Test receiving controller
[HttpPost]
public JsonResult getContentArray(List<Content> myPassedArray)
{
var data = myPassedArray;
return this.Json(null);
}
I got this working by set the traditional property to true before making the get call. i.e.:
jQuery.ajaxSettings.traditional = true
$.get('/controller/MyAction', { vals: arrayOfValues }, function (data) {...
I found the solution here: Pass array to mvc Action via AJAX
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With