0

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!

10
  • What type of field is gsGasoline Commented Nov 1, 2012 at 10:58
  • Could you please also include the code for the Action on your controller, the C# class that you try to map to, and the HTML markup for .gsGasoline? Commented Nov 1, 2012 at 10:58
  • 1
    If gsGasolineField is a checkbox, that will return "on" or "off", not a boolean. Try (gsGasolineField === 'on' ? true : false) or just (gsGasolineField === 'on') Commented Nov 1, 2012 at 11:01
  • I think jQuery.val() always returns a string, not a boolean. Commented Nov 1, 2012 at 11:01
  • @Barmar - It does, it's always a string, and stringifying it most certainly makes it a string, so why check for a boolean on the serverside ? Commented Nov 1, 2012 at 11:05

2 Answers 2

1

Change $('.gsGasoline').val() to $('.gsGasoline').is(':checked')

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.