11

I am using the HtmlHelper to create a checkbox in my view like so:

<%= Html.CheckBoxFor(model => model.SeatOnly, new { checked = "checked" })%>

However, an error is being thrown as checked is a reserved keyword. I have found a couple of people saying that you must use the 'reserved word prefix' and simply put an uderscore in front of the attribute like so:

<%= Html.CheckBoxFor(model => model.SeatOnly, new { _checked = "checked" })%>

This does not generate an error but in the generated html the attribute is actually '_checked' which means it doesn't work (if I use firebug and remove the underscore the attribute then takes effect).

Does anyone know a way around this while still using CheckBoxFor?

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
jcvandan
  • 14,124
  • 18
  • 66
  • 103

4 Answers4

17
<%= Html.CheckBoxFor(model => model.SeatOnly, new { @checked = "checked" })%>

You need to prefix it with an '@' :-)

Update

Just tested with the following and works. Try this ...

Model

public class MyModel
{
    public bool SomeBooleanValue { get; set; }
    public string Title { get; set; }
}

View (snipped to only the important bits)

<%= Html.TextBoxFor(x => x.Title) %>

<%= Html.CheckBoxFor(x => x.SomeBooleanValue, new { @checked = "checked" }) %>
WestDiscGolf
  • 4,098
  • 2
  • 32
  • 47
  • 1
    Thanks for the quick reply, I tried prefixing with @ but this doesn't seem to be working either, there is simply no 'checked' attribute in the generated html! – jcvandan Apr 22 '10 at 09:13
  • With my updated answer, I get the following Html outputted ... > – WestDiscGolf Apr 22 '10 at 10:03
  • Thats exactly the same way as I was attempting to do it, but for some reason it always emits the checked attribute from the html (but not other attributes e.g. class). I am truly stumped so I have decided to use CheckBox instead of CheckBoxFor. – jcvandan Apr 22 '10 at 10:06
  • 1
    I think model is overriding the value for the checkbox. Add a codesection above the checkboxes to set the defaults `@{ Model.SomeBooleanValue = true; }` – Brad Jan 18 '12 at 22:09
4

I had the same problem: the "checked" attribute was always omitted in the html code. i solved the problem by passing "true" into the model. snippet looks like this:

MyModel model = new MyModel();
model.SomeBooleanValue = true;

As you connect the Checkbox with the Model the checkbox always shows the value the model has. If the value in your model is "false" the checkbox will always be unchecked.

Maybe this helps ..

mikoku
  • 41
  • 1
0

Use @checked

Also occurs with the attribute class (reserved word), them use @class

Andersson Melo
  • 764
  • 1
  • 13
  • 32
0

I couldn't figure out a way to get the HtmlHelper to register the checked attribute so I simple used Html.CheckBox("SeatOnly", true) instead. This way the property in my view model is still being set and the checkbox is checked.

This is fine but I would still be interested in knowing why @checked doesn't work in the same way as @class.

jcvandan
  • 14,124
  • 18
  • 66
  • 103