Add Id or other attributes to ASP.NET MVC 3 Html Helper Textbox
How can I add开发者_如何学Go attributes to a html helper textbox.
I've tried this:
@Html.TextBox("username", new { id = "username" })
This seems to put 'id=username' in the value field of the textbox. I want to add an Id to my textbox.
Thanks.
The second parameter (new { id = "username" }
in your example) is the initial value (value attribute) of the TextBox. The third parameter is the actual htmlAttributes:
@Html.TextBox("username", Model.Username, new { id = "username" })
while new { id = "username" }
as the 2nd parameter is valid, you will need to add @
to attributes that are also keywords, like class
.
精彩评论