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");
}