When using Html.TextBoxFor, add the name of the model as prefix to id

34,713

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; }
Share:
34,713
Kobi
Author by

Kobi

Updated on August 09, 2020

Comments

  • Kobi
    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
    tugberk over 12 years
    your code will still generate name attribute as Name. So, this is not what he wants.
  • Hadas
    Hadas over 12 years
    It's not a problem. He can write: @Html.TextBoxFor(model => model.Name, new { @id = "UserName" ,@Name = "UserName"})
  • tugberk
    tugberk over 12 years
    That is completely wrong! You cannot override name attribute when you working with html helpers like TextBoxFor. BTW, FYI, you do not need @ escape char there. You use @ escape char with special C# keywords like class.
  • Kobi
    Kobi over 12 years
    i need it to be render automatically, and i dont want to change it in each view using this property.
  • gdoron is supporting Monica
    gdoron is supporting Monica over 12 years
    Won't work, The name of the property will be the name of the textbox
  • anar khalilov
    anar khalilov almost 11 years
    Could you please explain the "warning" part a little bit? Are form fields not already saved on postback in MVC?
  • Krisztián Balla
    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
    Krisztián Balla over 10 years
    This is exaclty the same as the most popular (until I downvoted it) answer of user1958148 suggested.
  • enoshixi
    enoshixi over 10 years
    Values 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
    DeadlyChambers about 8 years
    WOW, I was trying to override the name in the html attributes and it wasn't working because of lower case name. Thank you.
  • TSmith
    TSmith over 4 years
    If 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.