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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With