Reputation: 3242
I'm trying to post a JSON object to a Web Api URL and it's not binding to the model.
This seems to be the same problem: ASP.Net Web Api not binding model on POST
I tried everything that they did and it still doesn't work. The one difference you may notice is that I'm not using the DataContract
attributes, but I don't believe they should be required, and didn't make any difference when I tried them.
public class MyModel
{
public int Id { get; set; }
}
Public class MyController : ApiController
{
public int Save(MyModel myModel)
{
// myModel is always null
return 0;
}
}
Upvotes: 2
Views: 3827
Reputation: 6110
It could be an encoding issue. I changed my encoding and model binding is done succesfully.
client.Encoding = Encoding.UTF8;
Upvotes: 0
Reputation: 34238
You appear to be missing It appears in the above case this is actually not strictly required, perhaps this is only needed when posting primitives?[HttpPost]
attribute from your controller method.
Also just as a note I would use a more REST based syntax if you are using WebApi for example use methods Get, Post, Put ect on your controller rather than named methods
EDIT:
You also have one other really subtle issue with your post. A header line cant end with a ;
so Content-Type: application/json; charset=utf-8;
should be Content-Type: application/json; charset=utf-8
Upvotes: 5