When I send a list of int's with jQuery like this:
$.ajax('@Url.Action("Execute")', {
type: 'POST',
data: {
pkList: [1,2,3]
}
});
Then jQuery will transform the pkList object and send it by post like this:
pkList[]:1
pkList[]:2
pkList[]:3
Which would be fine if the server is PHP but I use Asp.NET MVC3 and try to get these values with the default model binder:
public ActionResult Execute(ICollection<int> pkList)
But pkList is always null, it seems that the default model binder cannot bind it.
How do I solve this correctly?
ADDED SOLUTION
I used the solution from Darin Dimitrov with setting the traditional
option in jQuery:
$.ajax('@Url.Action("Execute")', {
type: 'POST',
traditional: true,
data: {
pkList: [1,2,3]
}
});
Now jQuery doesn't add the []
to the parameters anymore and they are sent like this:
pkList:1
pkList:2
pkList:3
And the MVC default model binder gets the values correctly.
Hope this helps someone.
data: { pkList: '1,2,3' }
? But otherwise I use this method for all of my model serialization, the way to go for more complex objects in JS. – Nevels