For my curiosity, where did I miss it ? Was it documented on a famous blog post ?
I don't know if it was really documented anywhere, unfortunately. :( The issue is that we pulled some of the MVC Toolkit code into the MVC binary for Preview 3 and reimplemented some of the methods to match our coding standards more closely. The issue
you're seeing is that we pulled both TextArea and TextBox in from the toolkit, but we only had time to reimplement TextBox using the new syntax.
I am using Preview 3 and the following works just fine in my .aspx page (VB.NET):
<%
=Html.TextBox("Address2", ViewData.Model.Address2, CommonAttributes)%>
Where... CommonAttributes is a property in the code behind:
Protected
ReadOnly
Property CommonAttributes()
As Generic.IDictionary(Of
String,
Object)
Get
Dim attribs
As
New Generic.Dictionary(Of
String,
Object)
attribs.Add(
"class",
"ctrlTextBox")
Return attribs
End
Get
End
Property
To add a CSS class attribute to a HTML element generated by ASP.NET MVC's Html helper methods, as above, the preferred solution is to pass a htmlAttributes object into the element class constructor -- setting the @class parameter in the object initialiser.
An alternative solution would be to use the Insert extension method, if you find that more manageable. I haven't benchmarked other approaches and cannot make any assumptions about the relative performance or scalability of different approaches to this; it
would be interesting to compare performance:
<td class="data">
<%
Dim att As New Dictionary(Of String, Object)
att.Add("style", "width:225px;")
%>
<%=Html.TextBox("Email", Model.Email, att)%>
</td>
I use the @, and get the following error. Can you help?
Thanks.
Parser Error
Description: An
error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: "class" is a reserved word and cannot be used in implicit expressions. An explicit expression ("@()") must be used.
Source Error:
Line 19: <td><%=field.Attribute("name").Value %></td>
Line 20: <td>
Line 21: <%=Html.TextBox(field.Attribute("name").Value, field.Value, new {@class = field.Attribute("class").Value })%>
visualhint
Member
10 Points
25 Posts
Html.Textbox and the class html attribute
Jul 02, 2008 03:33 PM|LINK
Hi,
Before preview 3, this used to work:
<%= Html.TextBox("datetime", "", 10, 16, new { _class = "field" })%>
Now I have to replace it with:
<%= Html.TextBox("datetime", "", new { _class = "field" })%>
But the issue is not with the disappeared arguments. It is with the fact that the produced html is _class = "field" instead of class = "field".
It works well with a textarea but not with a simple textbox.
Thanks
htmlhelper HtmlAttributes textbox
Wargrip
Member
8 Points
7 Posts
Re: Html.Textbox and the class html attribute
Jul 02, 2008 04:26 PM|LINK
You need to use an "@" sign when you refer to attributes that are reserved words.
i.e. @class = "mycssclass"
visualhint
Member
10 Points
25 Posts
Re: Html.Textbox and the class html attribute
Jul 02, 2008 04:32 PM|LINK
Thanks a lot for this information.
For my curiosity, where did I miss it ? Was it documented on a famous blog post ?
Thanks
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Html.Textbox and the class html attribute
Jul 02, 2008 05:29 PM|LINK
I don't know if it was really documented anywhere, unfortunately. :( The issue is that we pulled some of the MVC Toolkit code into the MVC binary for Preview 3 and reimplemented some of the methods to match our coding standards more closely. The issue you're seeing is that we pulled both TextArea and TextBox in from the toolkit, but we only had time to reimplement TextBox using the new syntax.
dragthor
Member
17 Points
10 Posts
Re: Html.Textbox and the class html attribute
Jul 29, 2008 09:28 PM|LINK
I am using Preview 3 and the following works just fine in my .aspx page (VB.NET):
<%
=Html.TextBox("Address2", ViewData.Model.Address2, CommonAttributes)%> Where... CommonAttributes is a property in the code behind: Protected ReadOnly Property CommonAttributes() As Generic.IDictionary(Of String, Object) Get Dim attribs As New Generic.Dictionary(Of String, Object)attribs.Add(
"class", "ctrlTextBox") Return attribs End Get End PropertyMVC
http://www.kriskrause.com
htttp://www.dotnetmeister.com
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Html.Textbox and the class html attribute
Jul 29, 2008 11:00 PM|LINK
Because you're using an actual dictionary rather than an anonymous object, you won't run into the same syntactical issues as the original poster.
Tim Acheson
Member
40 Points
28 Posts
Re: Html.Textbox and the class html attribute
Jun 23, 2009 10:21 AM|LINK
To add a CSS class attribute to a HTML element generated by ASP.NET MVC's Html helper methods, as above, the preferred solution is to pass a htmlAttributes object into the element class constructor -- setting the @class parameter in the object initialiser.
An alternative solution would be to use the Insert extension method, if you find that more manageable. I haven't benchmarked other approaches and cannot make any assumptions about the relative performance or scalability of different approaches to this; it would be interesting to compare performance:
ASP.NET MVC asp.net CSS attribute html form element Insert class field
sghimire
Member
2 Points
1 Post
Re: Html.Textbox and the class html attribute
Mar 04, 2010 12:43 AM|LINK
<td class="data"> <% Dim att As New Dictionary(Of String, Object) att.Add("style", "width:225px;") %> <%=Html.TextBox("Email", Model.Email, att)%> </td>MVC - HTML TEXTBOX HTML ATTRIBUTE
davidatAndre...
Member
2 Points
1 Post
Re: Html.Textbox and the class html attribute
Apr 27, 2012 06:59 PM|LINK
I use the @, and get the following error. Can you help?
Thanks.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: "class" is a reserved word and cannot be used in implicit expressions. An explicit expression ("@()") must be used.
Source Error:
Line 19: <td><%=field.Attribute("name").Value %></td> Line 20: <td> Line 21: <%=Html.TextBox(field.Attribute("name").Value, field.Value, new {@class = field.Attribute("class").Value })%>MasterV23
Member
113 Points
318 Posts
Re: Html.Textbox and the class html attribute
Apr 27, 2012 07:09 PM|LINK
For class setting you would just need to do new { @class = "cssClass" }