Reputation: 7829
I have an MVC3 controller action:
public ActionResult DoStuff(DoStuffModel model)
The DoStuffModel
looks like this:
public class DoStuffModel
{
public long SomeId { get; set; }
public List<string> Codes { get; set; }
}
In my jQuery I do this:
var postData = {
SomeId: 1,
Codes: ["code1", "code2", "code3"]
};
$.post(url, postData, function (data) {});
The URL is correct. The postData
looks like this when I log it:
The SomeId
gets bound correctly, but Codes
remains null. What is going on?
Upvotes: 0
Views: 54
Reputation:
You need to use the $.ajax()
method and set the correct ajax options and stringify your data.
The $.post()
method is sending the data as application/x-www-form-urlencoded; charset=UTF-8
, which means you would need to generate the collection with indexers in order for the DefaultModelBinder
to bind it - for example
var postData = { SomeId: 1, 'Codes[0]': 'code1', 'Codes[1]': 'code2', 'Codes[2]': 'code3'};
The code using your current object containing an array would be
$.ajax({
url: url,
type: 'post',
contentType: 'application/json',
data: JSON.stringify({ model: postData })
success: function(data) {
....
}
});
Upvotes: 1