I've difficult to used new { attributes} in an HtmlHelper function. I'd like, for instance, t开发者_如何学JAVAo widen a TextBox. Should I use new { width = "50px"}, new { width = "50"}, or new { width = 50}, etc.
How many atttribues can I use?
What's the general rules?
Thanks helping
When adding HtmlAttributes in such a way it is important to keep in mind that the attributes you specify will get rendered as the html element's attributes. E.g. if you have:
<%= Html.TextBoxFor(... , new { width = "50px" }) %>
it will get rendered as
<input type="text" ... width="50px" />
In other words - exactly what you specify. Thus if you feel that it's difficult to decide what to write in the Helper, try to think what you want your Html to look like first, and go from there. You might want something like:
<%= Html.TextBoxFor(... , new { style = "width:50px;attribute2:value2;" }) %>
to get
<input type="text" ... style="width:50px;attribute2:value2;" />
But generally it is considered a good practice to separate layout from markup, so it's quite common to just apply a css-class to the element (note the @
before the class name - it is required due to class
being a keyword in C#):
<%= Html.TextBoxFor(... , new { @class = "mytextbox" }) %>
Where mytextbox is defined in a css file and specifies your width and other properties:
.mytextbox { width: 50px; attribute2: value2; ... }
精彩评论