3

This seems ridicouous but can you only return true/false from an MVC checkbox? I remember back in the day (in classic asp) it was perfectly valid to return a value from a ticked checkbox but Microsoft appears to of decided that checkboxes should only contain true/false value.

So if you add the following:

@Html.CheckBox("PropertyTypes", true, new {@value="A"})

I would expect my form POST to contain Property=A, what I end up with isProperty=A&Property=true which then breaks the model so I get a null in the property rather than the string I wanted.

On investigation it turns out this is because microsoft adds an extra input for so I get a false if I don't want to check the input...gee thanks a lot......:/

<input id="PropertyTypes" type="checkbox" value="Hotel" name="PropertyTypes" data-val-youmustselectatleastone="You must select at least one Property Type" data-val="true" checked="checked">
<input type="hidden" value="false" name="PropertyTypes">

WHY!

I could just code in an input myself just using flat HTML but then I loose all my unobtrusive validation.

any ideas on how to get around this stupidity?

Ps the number of checkboxes is dynamic so adding a property for each value isn't going to cut it.

9
  • @KristianHellang How can I return a value from a tickbox? Not true or false? Commented Jun 3, 2013 at 13:23
  • A checkbox is either checked/unchecked, e.g. true/false. Sure you don't want a dropdown? ;) Commented Jun 3, 2013 at 13:23
  • You have to create an HTML extension that adds validation attributes I think. Commented Jun 3, 2013 at 13:24
  • @KristianHellang No, it's a UI thing. I want x number of tick boxes that the user can tick that then decide on what options they are searching for. So I want a hotel and/or a villa and/or a apartment, etc. Commented Jun 3, 2013 at 13:25
  • 1
    And here's the comment from source code: "Render an additional <input type="hidden".../> for checkboxes. This addresses scenarios where unchecked checkboxes are not sent in the request. Sending a hidden input makes it possible to know that the checkbox was present on the page when the request was submitted." Commented Jun 3, 2013 at 13:30

1 Answer 1

8

This seems ridicouous but can you only return true/false from an MVC checkbox?

I don't find it ridiculous. When you give it a second thought modeling a checked/unchecked state with a boolean variable actually makes sense.

WHY!

Because that's how the designers of the framework decided to implement the CheckBox helper.

any ideas on how to get around this stupidity?

By using a view model of course:

public class MyViewModel
{
    public int Id { get; set; }
    public bool IsChecked { get; set; }
}

and then assuming you had a collection of those you would use a checkbox and a hidden field to store the corresponding id of the item:

@Html.HiddenFor(x => x.SomeCollection[i].Id)
@Html.CheckBoxFor(x => x.SomeCollection[i].IsChecked)

When the form is submitted you will get the collection property containing the ids of your items and whether they were selected or not.

Sign up to request clarification or add additional context in comments.

5 Comments

So this breaks the W3C standard...w3.org/TR/html401/interact/forms.html Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property. The INPUT element is used to create a checkbox control.
No, this doesn't break any W3C standard. Feel free to pass your generated markup through the W3C validator and you will be happy :-)
Well thanks for the answer...I'm really angry at this for some reason....I may write MS an angry letter...that'll teach them! :)
Go ahead. You could also say a Welcome from me in your letter and tell them to remove ViewData/ViewBag in the next release :-)
Thank you so much, I read about 10 different pages before finding this one. You explain it so simply. Now I can go and do some real work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.