12

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="" />
gdoron
  • 147,333
  • 58
  • 291
  • 367
Kobi
  • 181
  • 1
  • 2
  • 10
  • 4
    If you want it to be called UserName in the view, why not call it UserName in the model? – Øyvind Bråthen Jan 02 '12 at 08:20
  • I second Oyvind's comment, why have a property, called Name, decorated with an attribute that forces "Name", yet you want it to render as "UserName"? Seems strange to me. – enorl76 Jan 02 '12 at 19:02
  • it is an option to change the name of the property, but if there is a way to put some attribut for all the html hrlpers so it was much better and easier. – Kobi Jan 05 '12 at 22:26

6 Answers6

12
@Html.TextBoxFor(model => model.attr, new { Name = "txt1" })

Just Use "Name" instead of "name"

u_1958148
  • 383
  • 3
  • 10
  • 2
    -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 Aug 09 '13 at 08:51
  • 1
    WOW, I was trying to override the name in the html attributes and it wasn't working because of lower case name. Thank you. – DeadlyChambers Apr 16 '16 at 16:40
4

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.

enoshixi
  • 309
  • 3
  • 13
  • Could you please explain the "warning" part a little bit? Are form fields not already saved on postback in MVC? – anar khalilov Jul 14 '13 at 10:10
  • 2
    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. – enoshixi Oct 14 '13 at 21:33
  • 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. – TSmith Aug 29 '19 at 16:03
2

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; }
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

If I understood your question correctly, you want to be able to prefix a string onto the Model's property name.

Therefore if you are in the view 'Account', you will get

AccountName

whilst in the view 'Test', you will get

TestName

If that is the case, I think your best bet is overriding the PartialFor extension method, similarly as is done here:

ASP.NET MVC partial views: input name prefixes

In this example, it will render

Account.Name and Test.Name
Community
  • 1
  • 1
Kevin Farrugia
  • 6,431
  • 4
  • 38
  • 61
0

You should change your DisplayName attribute from "Name" to "UserName".

[DisplayName("UserName")]
public string Name{ get; set; }

If you do that, you'll get what you're asking for.

enorl76
  • 2,562
  • 1
  • 25
  • 38
-2

You can write:

@Html.TextBoxFor(model => model.Name, new { @id= "UserName",@Name="UserName" })
Hadas
  • 10,188
  • 1
  • 22
  • 31
  • your code will still generate `name` attribute as **Name**. So, this is not what he wants. – tugberk Jan 02 '12 at 10:21
  • It's not a problem. He can write: @Html.TextBoxFor(model => model.Name, new { @id = "UserName" ,@Name = "UserName"}) – Hadas Jan 02 '12 at 10:37
  • 2
    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`. – tugberk Jan 02 '12 at 10:50
  • i need it to be render automatically, and i dont want to change it in each view using this property. – Kobi Jan 02 '12 at 10:51
  • This is exaclty the same as the most popular (until I downvoted it) answer of user1958148 suggested. – Krisztián Balla Aug 09 '13 at 08:53