Reputation: 95
I have seen many similar questions and I have tried all the possible answers but my problem is still the same. Basically, I have the following ajax call.
var obj = {
Duration: 12000,
Errors: 2
};
var post = $.ajax({
url: APIUrl,
data: JSON.stringify(obj),
type: "POST",
cache: false,
dataType: 'json',
contentType: 'application/json, charset=utf-8'
});
This is my View Model.
public class TargetBasedViewModel
{
public int Duration{ get; set; }
public int Errors{ get; set; }
}
And here is my controller.
[HttpPost]
public HttpResponseMessage Post(TargetBasedViewModel test)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
The problem is that the parameter keeps coming as null. I have used Fiddler to examine the request and it looks fine, all the attributes are there and I can even examine the JSON object and make sure that my values are actually being sent to the server. I have read many articles and even tried to add [ModelBinder] and [FromBody] to the parameter, but still no luck. I have also tried to add this to my Application_Start()
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings();
Any ideas what I might be doing wrong?
Upvotes: 2
Views: 231
Reputation: 13975
You might look at this post and the accepted answer. It suggests that:
I THINK I'VE GOT IT! Your contentType parameter:
It needs to be "application/json;charset=utf-8"
You've got a COMMA after the "json".
Upvotes: 1
Reputation: 12405
Check the ModelState property on your controller. Deserialization errors are recorded there. You'll likely find out why deserialization failed.
Upvotes: 0