Reputation: 9583
Posting an array of ints
to my MVC
controller seems to result in the value being null
.
The form data (parsed), indicates that each element in the array is posted individually:
ids[]:39
ids[]:54
Which seems to make sense as I read that form data can only be posted as key/value pairs.
My controller action is defined as follows:
[HttpPost]
[Authorize]
public JSONResult SubmitApprovedUploadedPhotoIds(List<int> ids)
{
try
{
if (ids == null)
throw new ArgumentNullException("ids");
// ...
}
// ...
}
The main portion of my ajax POST:
// `ids` is an array of ints
$.ajax({
url: "/MyController/SubmitApprovedUploadedPhotoIds",
cache: false,
data:
{
ids: ids
},
dataType: "json",
type: "POST",
});
I would like the array of posted ids to populate the ids
variable in the MVC
action.
I have tried changing the post code to:
{
ids: $(ids).serializeArray()
},
and have tried changing my MVC
action to:
public JSONResult SubmitApprovedUploadedPhotoIds(int[] ids)
with no success.
Upvotes: 0
Views: 34
Reputation: 1080
To send a list, I think you'll have to make your data go from this:
ids[]:39
ids[]:54
To this:
ids:39
ids:54
In your ajax/post call, try adding traditional: true
so it gets rid of the []
Refer to this for more questions: How to send a list of int with jQuery to ASP.net MVC Default Model Binder
Upvotes: 1