0

I don't why the Html.CheckBoxFor(x => x.IsChecked) helper was implemented. Why does it force you to have to use a bool value?

From what I seen the regular html input can have a "value" of any string. So why does the html helper limit you?

I am having a problem right now where I would love to change the "value" to store my GUID but since it only takes in a bool I can't do this.

I see other people make a HiddenFor() to get around this but I just find this weird.

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • Here is an answer where someone created a own extension, it maybe is usefull for you: http://stackoverflow.com/questions/7195846/how-can-i-make-html-checkboxfor-work-on-a-string-field – Simon Edström Mar 23 '12 at 18:01

1 Answers1

0

It's because usually a checkbox has 2 states: checked and unchecked. Which is perfectly modeled by a boolean. Now I understand your pain and agree with you that this could be a bit of a limitation because the underlying <input> HTML element allows for potentially any type. But this limitation is very easily workarounded by simply adding a boolean property to your view model and then bind the CheckBoxFor helper to this property.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • To state it a slightly different way, the `CheckboxFor()` helper is binding a Boolean to the checkbox *state*, not *value*. – GalacticCowboy Mar 23 '12 at 18:43
  • @Darin Dimitrov -Well that is what I have right now I have a bool in my ViewModel for it. It just weird that I have to use HiddenFor() to hold the ID so I know that as well. Where it would have been nice it would all be in one control. – chobo2 Mar 23 '12 at 20:14
  • @chobo2, I agree with you, it could be convenient in some situations to be able to bind to another type than bool. – Darin Dimitrov Mar 23 '12 at 21:13