When using Html.TextBoxFor, add the name of the model as prefix to id
Solution 1
@Html.TextBoxFor(model => model.attr, new { Name = "txt1" })
Just Use "Name" instead of "name"
Solution 2
If you don't want to render the input as described in your model, you probably don't want to be using the Html.TextBoxFor
helper.
Try Html.TextBox
instead. You can provide it with the exact values you're looking for:
@Html.TextBox("UserName", Model.Name, new { id = "UserName" })
Don't forget you can also forget the helpers and use plain old html:
<input id="UserName" name="UserName" type="text" value="@Model.Name" />
Warning: When using either of these methods and the default model binder, the value of this input will not be bound back to your model correctly when submitting back the data.
Solution 3
The TextBoxFor helper uses the lambda expression that is passed as first argument to calculate the name of the input field. So if you want the generated input field to be called UserName
, simply rename the property in your view model:
[DisplayName("Name")]
public string UserName { get; set; }
Kobi
Updated on August 09, 2020Comments
-
Kobi over 3 years
I have an asp mvc 3 project When I use
@Html.TextBoxFor(model => model.Name)
the TexBoxFor render as
<input id="Name" name="Name" type="text" value="" />
my model is "UserModel" :
[DisplayName("Name")] public string Name{ get; set; }
Is there a way to add to the name of the model as prefix in the id? maybe some attribute?
I want that the text box will render as
<input id="UserName" name="UserName" type="text" value="" />
-
tugberk over 12 yearsyour code will still generate
name
attribute as Name. So, this is not what he wants. -
Hadas over 12 yearsIt's not a problem. He can write: @Html.TextBoxFor(model => model.Name, new { @id = "UserName" ,@Name = "UserName"})
-
tugberk over 12 yearsThat is completely wrong! You cannot override
name
attribute when you working with html helpers likeTextBoxFor
. BTW, FYI, you do not need@
escape char there. You use@
escape char with special C# keywords likeclass
. -
Kobi over 12 yearsi need it to be render automatically, and i dont want to change it in each view using this property.
-
gdoron is supporting Monica over 12 yearsWon't work, The name of the property will be the name of the textbox
-
anar khalilov almost 11 yearsCould you please explain the "warning" part a little bit? Are form fields not already saved on postback in MVC?
-
Krisztián Balla over 10 years-1: This will result in two attributes called name: Name="txt1" and name="attr". It does work in Firefox, as the browser ignores the second name-attribute. But this is esentially a botch.
-
Krisztián Balla over 10 yearsThis is exaclty the same as the most popular (until I downvoted it) answer of user1958148 suggested.
-
enoshixi over 10 yearsValues are bound to models based on the input names. So in this case the model binder doesn't know that the value from the "UserName" input corresponds to the "Name" value on the model.
-
DeadlyChambers about 8 yearsWOW, I was trying to override the name in the html attributes and it wasn't working because of lower case name. Thank you.
-
TSmith over 4 yearsIf one is trying to change the input's name attribute after binding to the model, the one probably already knows they are not going to bind back to that same model. A likely scenario is if you have a model, but are only binding to one or two properties and you do that via two parameters on a controller method.