Friday 30 December 2011

ASP.NET MVC : Using C# keyword 'class' as HTML attribute

If you are developing an ASP.NET MVC application you will have to set the CSS class name of an HTML element or INPUT control using HTML Helper methods. For example


@Html.RadioButton("status", "1", true, new { id = "status", class='radio' })


tries to set the CSS class name of the radio button to 'radio'. C# will not allow to do this as "class" is a C# keyword. You have to prefix "class" with an @ to fix this.


@Html.RadioButton("status", "1", true, new { id = "status", @class='radio' })


will work.

No comments:

Post a Comment