I am attempting to make a PUT call to my Web API. I have set the following up in the WebApiConfig.cs to handle sending data back to my Web project in camel case.
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver
= new CamelCasePropertyNamesContractResolver();
My data is coming from a row in a Kendo UI Grid. The javascript that makes the Ajax call is below:
...
save: function(e) {
var dataItem = e.model;
$.ajax({
url: apiUrl() + "/roleLocationSecurable",
type: 'PUT',
headers: {
'Authorization': 'Bearer ' + getTwtToken(),
'LocationId': getCurrentLocation()
},
data: JSON.stringify(dataItem),
dataType: 'json'
}).done(function(data){
//stuff when finished
});
}
...
The data being sent to the Web API looks like this:
"id":1,
"roleLocationId":5
"moduleId":5
...
The Action on my Web API 2.0 Controller looks like this:
[HttpPut]
[Route("roleLocationSecurable")
public IHttpActionResult UpdateRoleLocationSecurable(RoleLocationSecurableResource model)
{
var roleLocationSecurable = Mapper.Map<RoleLocationSecurableResource, RoleLocationSecurable>(model);
if(_roleService.UpdateRoleLocationSecurable(roleLocationSecurable))
{
return Ok();
}
return NotFound();
}
And the Model my controller is expecting has properties named like this:
public int? Id {get;set;}
public int RoleLocationId {get;set;}
public int ModuleId {get;set;}
....
What's happening is that my Ajax call is firing and the body of my PUT call includes all of the information from above, with the camel case keys. The Web API Controller is being called, but the values in my model are not populated with anything (the model is essentially blank).
The issue I think I have (and maybe I'm wrong) is that the Action on my Web API controller is expecting Pascal Case properties, and my Ajax call is sending Camel Case.
Since the CameCasePropertyNamesContractResolver is handling the serialization of data to camel case, why isn't it handling the deserialization? Is there a simple, easy way to fix this?
Any help on why the above code isn't working would be greatly appreciated.