Reputation: 207
What I'm trying to do is to pass JSON object to a WebAPI ajax call and mapped to a strongly typed object on the server side. String values are being posted perfectly however when it comes to boolean values, they are not being passed at all. Below is my code:
var gsGasolineField = $('.gsGasoline').val();
blData = { Gasoline: gsGasolineField };
var json = JSON.stringify(blData);
$.ajax({
type: "POST",
url: url,
data: json,
contentType: "application/json",
dataType: "json",
statusCode: {
201 /*Created"*/: function (data) {
$("#BusinessLayerDialog").dialog("close");
ClearForm("#BusinessLayerForm");
},
400: /*Bad request - validation error*/ function (data) {
$("#BusinessLayerForm").validate().form();
},
500: function (data) {
alert('err');
}
},
beforeSend: setHeader
});
Gasoline property is of type boolean on the server side.
EDIT:
As mentioned above, Gasoline is boolean and being MVC my HTML markup is as follows
<div style="float: left">@Html.CheckBoxFor(x => x.GasStation.Gasoline, new { @class = "gsGasoline" })</div>
So I'm just taking the values of this checkbox and passing it to the JSON object
EDIT
Also tried to to send it true directly
blData = { Gasoline: true };
Still false server side!
Upvotes: 0
Views: 6583
Reputation: 207
The problem turned out to be due to the inheritance aspect of my server side architecture. All properties in the parent class are being deserialized correctly and everything in the child class remains untouched. Nothing was related to the type passed to the JSON.Stringify function.
Thank you all for your help.
Upvotes: 0