Dietpixel
Dietpixel

Reputation: 10123

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.

Upvotes: 4

Views: 31560

Answers (2)

AceMark
AceMark

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

Bryan Menard
Bryan Menard

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

Related Questions