0

I thought MVC3 can bind JSON data to model by default.

but this code

server:

[HttpPost]
public ActionResult Save(IList<int> IDs)
{
    return null;
}

client:

$.post('@Url.Action("Save", "Users")', {'IDs' : [1, 2, 3]}, function() {});

don't work. Why ??

Jack128
  • 1,113
  • 10
  • 15

4 Answers4

2

You need to send your data as application/json:

$.ajax({
    type: 'post',
    url: '/Users/Save'
    data: JSON.stringify({'IDs' : [1, 2, 3]}),
    contentType: 'application/json; charset=utf-8',
    success: function() {
       // ...
    }
});
Dismissile
  • 32,564
  • 38
  • 174
  • 263
2

Your code sends IDs[]=1&IDs[]=2&IDs[]=3.

You need send IDs=1&IDs=2&IDs=3.

Set traditional:true parameter to use the traditional style of param serialization.

$.ajax({
    url: '@Url.Action("Save", "Users")',
    type: 'post',
    data: {'IDs' : [1, 2, 3]},
    traditional:true,
    success: function() {
        // ...
    }
})
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
Bohdan Lyzanets
  • 1,652
  • 22
  • 25
0

This might be the same as the problem I ran into a while ago. Check out this SO question Post Array as JSON to MVC Controller

Community
  • 1
  • 1
Tim B James
  • 20,084
  • 4
  • 73
  • 103
0

You have to apply JSON.stringify

$.post('@Url.Action("Save", "Users")', JSON.stringify({'IDs' : [1, 2, 3]}), function() {}); 
VJAI
  • 32,167
  • 23
  • 102
  • 164