Reputation: 10123
How can I add 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.
Upvotes: 4
Views: 31560
Reputation: 729
while new { id = "username" }
as the 2nd parameter is valid, you will need to add @
to attributes that are also keywords, like class
.
Upvotes: 1
Reputation: 13374
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" })
Upvotes: 8