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.
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 })
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