0

Is there anyway to work with multiple check boxes in MVC. I am getting both true and false values in query string when using the example below.I just want to pass if it was checked True or if it was not checked False.

'http://'webapp':5841/Result/Triggers?Alcohol=true&Alcohol=false&Awakening=true&Awakening=false&Coffee=true&Coffee=false'

public class Triggers
{
    public string Alcohol { get; set; }
    public string Awakening { get; set; }
    public string Coffee {get; set;}

}



 @using (HTML.BeginForm("Triggers", "Result", FormMethod.Get))
            {
                   '@Html.CheckBox("Alcohol")'

                    '@Html.CheckBox("Awakening")'

                    '@Html.CheckBox("Coffee")'
              }


    [HttpGet]
    public ViewResult Triggers()
    {
        return View();
    }
user2224493
  • 259
  • 3
  • 5
  • 13
  • Take a look here: http://stackoverflow.com/questions/5462967/razor-viewengine-html-checkbox-method-creates-a-hidden-input-why – Floremin May 07 '13 at 17:30

1 Answers1

3

This is actually by design.

The CheckBox() (and RadioButton()) helper generates a second <input type="hidden" name="yourName" value="False" />.

This makes sure that unchecked checkboxes are still sent back to the server for model binding.

If the checkbox is checked, the browser will send two values, and the model binder will use the second one.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964