0

I have an ASP.Net WebAPI project where I'm model binding a model on a Controller method. See Model and Controller method below.

The issue is when I set the IsActive flag to 333 (which is not a boolean), the model binding sets the IsActive field to true; I can see model.IsActive is true when debugging. If I set to a string, like "asdf", it fails, as expected (error: Could not convert string to boolean: asdf.). Other fields are being bound correctly.

Has anyone seen this issue before? Am I doing something wrong?

Model:

public class RequestModel
{
    [Required]
    public string Name { get; set; }

    [Required]
    public bool IsActive { get; set; }
}

Controller method looks like this (I set a breakpoint in the method and can see model.IsActive is true):

[HttpPost]
[Route("")]
public IHttpActionResult CreateStuff([FromBody] RequestModel model)
{
    if (ModelState.IsValid)
    {
        // Do stuff

        return Ok();
    }

    return BadRequest("error string");
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • It's probably the `[FromBody]`; that uses the JsonInputFormatter, and in JavaScript, 333 evaluates as "truthy". I'm not sure this is the problem, which is why this is not an answer. I'd take a look at [the docs](https://docs.asp.net/en/latest/mvc/models/model-binding.html). – Heretic Monkey Oct 05 '16 at 22:17
  • @MikeMcCaughan you are correct that the default JsonMediaTypeFormatter in web API is provided by the Json.NET library and will deserialize 0 to false and 1 or greater as true. If you don't like the this behavior you can write a custom JsonConverter, see [link](http://stackoverflow.com/questions/14427596/convert-an-int-to-bool-with-json-net) for an example. – Chris Morgan Oct 06 '16 at 19:25
  • @MikeMcCaughan, thanks the ideas! I tried removing the [fromBody] and that didn't fix the issue. We stood up a new, simple API project with a model that contained an int and a bool, and set the bool to 321. When the content-type request header was set to application/ajax, the bool was parsed to true, but when the header was set to application/x-www-form-urlencoded, the model binding broke. I then went back to my original project and tried changing the content-type to application/x-www-form-urlencoded, but the model binding set the model to null, so binding didn't work. – Dan Gilberstadt Oct 07 '16 at 16:27
  • Thanks for the comment, @ChrisMorgan – Dan Gilberstadt Oct 07 '16 at 16:28
  • The content-type header for a JSON request should be set to `application/json`, not `application/ajax`. I would stick with JSON, rather than form encoding as it's a lot easier to work with, and most tooling these days expect JSON. I'm not sure exactly what you're using to edit the IsActive value. I'd suggest using `@Html.CheckBoxFor(m => m.IsActive)` so that it's always a Boolean... – Heretic Monkey Oct 07 '16 at 16:33
  • @MikeMcCaughan, yes, I'm sorry, I meant application/json (not ajax). An outside system will be consuming this via the API, so we will not be using Razor and html helpers. – Dan Gilberstadt Oct 11 '16 at 01:26

0 Answers0