Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Id or other attributes to ASP.NET MVC 3 Html Helper Textbox

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.

like image 348
Dietpixel Avatar asked Jan 19 '23 21:01

Dietpixel


2 Answers

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" })
like image 117
Bryan Menard Avatar answered Jan 21 '23 09:01

Bryan Menard


while new { id = "username" } as the 2nd parameter is valid, you will need to add @ to attributes that are also keywords, like class.

like image 27
AceMark Avatar answered Jan 21 '23 11:01

AceMark