Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override the "id" attribute of Html.EditorFor - not working

Trying to override the "id" attribute of Html.TextBoxFor (MVC 3) so that it should look like:

<input type="text" name="Password" id="@idPasswordTextBox" value="@Model.Password" />

where "idPasswordTextBox" is defined as: string idPasswordTextBox = "passwordText_"[email protected]; in the same cshtml file.

This is working fine if I use as :

<input type="text" name="Password" id="@idPasswordTextBox" value="@Model.Password" />

but not working if I do it this way:

@Html.TextBoxFor(model => model.Password, new { id = "@idPasswordTextBox" })

Looks like the "id" attribute is getting messed up. What am I missing? Can anybody help? I am new bee in ASP.net.

Thanks in advance.

like image 888
VictorGram Avatar asked Sep 01 '11 18:09

VictorGram


1 Answers

Sorry, i should've looked more carefully. You don't want quotes around @idPasswordTextBox in your TextBoxFor method. That is run on the server, so when you put quotes around the name it is being treated as a literal string. Remove the quotes, and remove the @ sign in front of id, and it will work.

Its important to always remember what is running on the server, and what is running on the client.

@Html.TextBoxFor(model => model.Password, new { id = @idPasswordTextBox })
like image 82
Jason Avatar answered Oct 11 '22 17:10

Jason