Setting html attribute that is reserved keyword in Html.CheckBoxFor in ASP.NET MVC

Posted by dormisher on Stack Overflow See other posts from Stack Overflow or by dormisher
Published on 2010-04-22T08:54:10Z Indexed on 2010/04/22 10:03 UTC
Read the original article Hit count: 951

Filed under:
|
|

Hi, 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

© Stack Overflow or respective owner

Related posts about c#

Related posts about mvc