MVC + jQuery: how to create an input text through Html.TextBox() and specifying a "class" attribute
I'm trying to create an input text through the following code so it can hold a date that user can choose in a date-picker jQuery control:
开发者_如何转开发 <%: Html.TextBox("date", null, new {class = "date-pick"}) %>
<input name="date" id="date" class="date-pick" />
Problem is, I need to specify the "class" attribute for the input text, but "class" is of course a reserved keword in C# :-)
Is there any way to use this override for this?
Thanks!
Use @class
in cases such as this, it should look like this:
<%: Html.TextBox("date", null, new {@class = "date-pick"}) %>
For any attribute that's a reserved word, just prefix it with a @
.
For people lookign to set the value and happen to stumble across this question like I did, the same sort of code from Nick Craver's answer can be applied to setting the value of a text field too:
@Html.TextBoxFor(c => model.Propertyname, new {@Value = "5"})
The above must be used instead of the below or it will not work:
@Html.EditorFor(c => model.Propertyname, new {@Value = "5"})
You must also use a capital 'v' for Value
精彩评论