2

I have a PHP program POSTING to a C# MVC WebApi. The WebApi program starts as:

public async Task<IHttpActionResult> EmailExists(UsersEmailExistsBindingModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    /* remaining functionality */
}

The binding model as as follows:

public class UsersEmailExistsBindingModel 
{
    [Required]
    public string UserId { get; set; }

    [Required]
    public string Email { get; set; }

    public bool Update { get; set; }
}

The Update property represents if this is for an existing user or not. i.e Are we doing an update?

When PHP posts the request, it uses Update=1 in the form content, which returns The value '1' is not valid for Update.

How can I specify that 1 and 0 should be accepted as valid values for true and false for the boolean value when binding?

Reuben
  • 4,136
  • 2
  • 48
  • 57

1 Answers1

0

You can either

  1. Add a property with short, and then your bool property wrapping the short property.

  2. Implement a CustomValueProvider see: How to map a 1 or 0 in an ASP.Net MVC route segment into a Boolean action method input parameter

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
  • 1
    Solution 2 might be better for handling any location that I want to bind 1 and 0 for boolean values. However, ASP.Net MVC is not ASP.Net WebApi, and I'm not sure that their binding techniques are the same. – Reuben Mar 10 '15 at 00:57