but for some reason it includes the underscore in front of class: "_class". I can't simply say class because it's reserved but I thought this was the workaround? I've seen others try it in posts like :
In c# to use an identifier that clashes with a keyword you prefix it with an @ sign. So you should have
new {size=30, @class="required"}
**Warning, statement of the bleeding obvious coming - no offence intended. You don't have to use the helper, you can just use the html you have shown (some folks think it is cleaner that way).
Thank you! I was banging my head for hours trying to figure out how to do it in a non-hacky way. I'm new to C# too (Java is my main language). Is the @-style a convention used in non-MVC apps too?
But i have found that when I do ... new { Class = "foo" } it will render out to ... class="foo".
The HTML helpers were all originally part of the MVC toolkit, and we've been going through cleaning them up. If you're seeing this particular behavior in some helper, it just means that we haven't gotten around to that particular helper yet. They are (or
at least *should be*) case sensitive, and you can't depend on the Class -> class behavior.
cepcion
0 Points
2 Posts
htmlAttributes and css class?
Aug 12, 2008 10:28 PM|LINK
I've been developing an ASP.NET MVC application and have come across some odd behavior.
I have code like: <%= Html.TextBox("email", ViewData.Model.email, new { size = 30, _class = "required" })%>
and I'd like it to produce code like :
http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/
I was using the Preview 3 release and just updated to the Preview 4 release. Any hints on what I could be missing?
Paul Linton
Star
13403 Points
2531 Posts
Re: htmlAttributes and css class?
Aug 12, 2008 11:15 PM|LINK
In c# to use an identifier that clashes with a keyword you prefix it with an @ sign. So you should have
new {size=30, @class="required"}
**Warning, statement of the bleeding obvious coming - no offence intended. You don't have to use the helper, you can just use the html you have shown (some folks think it is cleaner that way).
<input size="30" class="required" type="text" name="email" id="email" value="<%=ViewData.Model.email%>" />
A third option would be to make your own overload which has a parameter for the css class.
hope that helps
levib
Star
7702 Points
1099 Posts
Microsoft
Re: htmlAttributes and css class?
Aug 13, 2008 01:30 AM|LINK
Remember to <%= Html.AttributeEncode(ViewData.Model.email) %> that data, my friend. :)
cepcion
0 Points
2 Posts
Re: htmlAttributes and css class?
Aug 13, 2008 03:42 AM|LINK
Thank you! I was banging my head for hours trying to figure out how to do it in a non-hacky way. I'm new to C# too (Java is my main language). Is the @-style a convention used in non-MVC apps too?
Paul Linton
Star
13403 Points
2531 Posts
Re: htmlAttributes and css class?
Aug 13, 2008 03:53 AM|LINK
It's nothing to do with MVC. This is part of C#
derikwhittak...
Member
10 Points
6 Posts
Re: htmlAttributes and css class?
Aug 24, 2008 01:14 AM|LINK
I have also found that in the case where the attribute clashes with a keyword you can simply UpperCase the attribute == class = Class.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: htmlAttributes and css class?
Aug 24, 2008 01:32 AM|LINK
You should probably not do this, since XHTML compliance requires case-sensitivity, and class and Class are not the same.
derikwhittak...
Member
10 Points
6 Posts
Re: htmlAttributes and css class?
Aug 24, 2008 01:39 AM|LINK
You are correct in terms of XHTML.
But i have found that when I do ... new { Class = "foo" } it will render out to ... class="foo".
So even though I use Class it works out to be class when rendered to html.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: htmlAttributes and css class?
Aug 24, 2008 01:52 AM|LINK
The HTML helpers were all originally part of the MVC toolkit, and we've been going through cleaning them up. If you're seeing this particular behavior in some helper, it just means that we haven't gotten around to that particular helper yet. They are (or at least *should be*) case sensitive, and you can't depend on the Class -> class behavior.
gicola
Member
2 Points
1 Post
Re: htmlAttributes and css class?
Feb 23, 2010 06:07 AM|LINK
Just use , new { @class = "button" }. I noticed this when I was generating classes from xsd using svcutil.exe a long time ago.