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!
(gsGasolineField === 'on' ? true : false)or just(gsGasolineField === 'on')jQuery.val()always returns a string, not a boolean.