Monday, April 11, 2011

How do you pass class as a key value pair of ASP.NET MVC htmlAttributes?

I would like to pass the html input class attribute in ASP.NET MVC for my input box using Html.TextBox("FullName", null, new{class="grey-input"}). But I think I can use the "class" keyword.

How can I bypass this issue?

From stackoverflow
  • yeh you need to use @class which allows you to use a reserved word.

    Html.TextBox("FullName", null, new{@class="grey-input"})
    

    So an example:

    <%= Html.TextBox("Name","Andy",new{@class="Test"}) %>
    

    Produces

    <input class="Test" id="Name" name="Name" type="text" value="Andy" />
    

    You could also create a custom Extension method for this also, but the above way is what I use.

0 comments:

Post a Comment