locked
true in JSON Payload always recognized as false in MVC4 API Controller RRS feed

  • Question

  • User1287290425 posted

    I have a restful web service made by mvc4 web api. When I test a POST call with a payload, all the true value of boolean are recognized as false in the controller.

    My Payload is as follows:

    {"OrderID":154903709,"EventID":8,
    "GuestList":
    [
    {"IsPrimary":true,"IsPartner":false},
    {"FirstName":"First","LastName":"Last","IsPrimary":true,"IsPartner":true}
    ]

    In my controller to accept call, I have the following class to deserialize the payload:

    [System.Web.Http.HttpPost]
    public HttpResponseMessage Create([FromBody]CreateTicketOrderRequest request)
    {
    }
    
    
    public class CreateTicketOrderRequest
    {
    public int OrderID {get; set;}
    public int EventID { get; set; }
    public List<Registrant> GuestList {get; set;}
    }


    This is Registrant class

    public class Registrant : Guest
        {
            public int RegistrantID { get; set; }
            public string Email { get; set; }
            public string Phone { get; set; }
            public int EventID { get; set; }
            public bool IsPrimary { get; set; }
            public string WhereToStay { get; set; }
            public string Company { get; set; }
            public bool IsPartner { get; set; }
        }



    When it is called, I look step by step into the code, IsPrimary and IsPartner are both false for these two elements. I am not sure what is wrong with it.

    Thanks!

    Tuesday, August 27, 2013 4:35 PM

All replies

  • User-1588480722 posted

    For some reason, the boolean properties in JSON are not matching with the contract (Registrant class). Are you sure its happening only for the boolean properties?

    Try this : Create a console application (or write a functional test), generate a http request using HTTPClient using the same contract classes (CreateTicketOrderRequest) and check the payload generated using fiddler or any other network monitoring tools.

    Wednesday, August 28, 2013 4:01 AM
  • User-1955523146 posted

    Did you ever figure this out? I'm dealing with a similar issue in WebAPI 2

    Wednesday, June 22, 2016 9:31 PM