Veggerby : IBlog | Low decoupling, highly incoherent

Nov/08

19

ASP.NET MVC HtmlHelper methods and CSS class

If you in ASP.NET MVC use a Html helper method to render out e.g. an input box:

<% = Html.TextBox("name")  %>

Sometimes you might need to add an CSS class attribute to your input box (this should be a fairly standard thing). For this there exist overload methods that takes a “htmlAttributes” parameter of type object. Now my experience with this is somewhat similar to Ayende Rahien experience – although I have not begun banging my head against the wall out of frustration. This is not a rant, I love ASP.NET MVC, I just had an experience with something simple that was hard to figure out because of some of the design decisions – not that they are bad, they can just be confusing – especially with the lack of documentation at the current stage (yes, I know it is just a beta). So if you try this:

<% = Html.TextBox("name", null, new{ class = "required"})  %>

You will (of course) get a compiler error, since “class” is a keyword and cannot be used like this. So how do you specify the class? Well, like this:

<% = Html.TextBox("name", null, new{ @class = "required"})  %>

Just for future reference :)

· ·

4 comments

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me