I am trying to send a jquery sortable list of items to my MVC method for data processing. Currently I am trying to send it via the following code:
var data = {};
data.projectId = projectId;
data.publishedSectionIds = $('#section_list').sortable('toArray');
// Perform the ajax
$.ajax({ url: '/Project/Publish',
type: 'POST',
data: data,
success: function (result) {
alert(result.message);
}
});
The problem with this code is it makes the Post parameters look like this:
projectId=2&publishedSectionIds[]=1&publishedSectionIds[]=2
The issue with this (as can be seen by the solution to this question) is that MVC only seems to serialize into a List if the post parameters do NOT have brackets.
How can I serialize the javascript array so my action with a List<int>
parameter model binds correctly?
public ActionResult Publish(int projectId, List<int> publishedSectionIds)
try adding the following option traditional: true
eg
$.ajax({ url: '/Project/Publish',
type: 'POST',
data: data,
traditional: true,
success: function (result) {
alert(result.message);
}
});
see
As of jQuery 1.4, the $.param() method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;.
http://api.jquery.com/jQuery.param/
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