Skip to content

Htmlhelpers Css Classes – mvc

January 29, 2009

This one had me scratching my head today. The jquery form validation uses the css classes to do it’s magic, so I wanted to add a class to a textbox, like this:

<%= Html.TextBox("Password", "", new {type = "password", maxlength="8", class="required"}) %>

This throws a compile error as class is a protected word !

The trick is to put an AT (@) sign in front of class !

<%= Html.TextBox("Password", "", new {type = "password", maxlength="8", @class="required"}) %>

Thanks to this blogpost for the answer:

One Comment
  1. November 10, 2010 4:20 pm

    You can also use an uppercase C: Class=”…”. Html doesn’t care.

Comments are closed.

%d bloggers like this: