8

Is it possible to control the name attribute of say a text boxt when using TextBoxFor?

this code in the view

@Html.TextBoxFor(m => m.SearchParams.someParam)

produce this

<input id="SearchParams_someParam" name="SearchParams.someParam" type="text" value="">

but I do not want my input name to be "SearchParams.someParam" and I wanted that to be something like

<input id="SearchParams_someParam" name="MyPreferedName" type="text" value="">

where the MyPreferedName comes from from some attributes the .SearchParams.someParam in the corresponding model.

Is this possible? I know @Html.TextBox does it but I do not want to hardcode the name in the view.

iCode
  • 4,308
  • 10
  • 44
  • 77
  • Duplicate of http://stackoverflow.com/questions/6064363/mvc2-impossible-to-change-the-name-with-textboxfor and http://stackoverflow.com/questions/6057865/asp-net-mvc-3-override-name-attribute-with-textboxfor – Fernando Correia Aug 20 '12 at 14:23
  • The 'name' is the name the field value is posted to the server with. If you're using the *For helper methods, you're passing a model property expression, and if you want the field to bind to that property in the model, then you need to use the right name -- the one MVC generates for you. There's no good reason to override it. The 'id' attribute can be overridden, because you can use it however you want in JavaScript, but the 'name' attribute serves a specific function in the MVC framework; it's used in model binding on post backs. For custom names, use the overload that.accepts a name/value. – Triynko Jul 19 '16 at 04:21

5 Answers5

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

Just Use "Name" instead of "name"

u_1958148
  • 383
  • 3
  • 10
  • 1
    Why does that work!! Oh well...this does work as long as the name property is "Name" – Agile Jedi Jan 29 '14 at 21:52
  • 1
    This doesn't stop `TextBoxFor` from outputting the `name` attribute... this just adds another attribute named `Name` as well... not what was requested. – Sheridan Apr 14 '15 at 14:06
  • By doing this, the values in your text box wont properly bind upon form submission. Your model ends up with null for the attr property. – Icestorm0141 May 20 '15 at 18:53
  • 2
    In ASP.NET 5 this renders as `` unfortunately. – Dai Jun 16 '16 at 10:45
5

It seems as though the TextBoxFor method will not allow you to use the @name key as an htmlAttribute. This makes a little bit of sense because if it did allow you to override the HTML name attribute, the value would not get binded to your model properly in a form POST.

Instead of using...

@Html.TextBoxFor(x => Model.MyProperty, new { @name = "desired_name" }); // does not work

I ended up having to use...

@Html.TextBox("desired_name", Model.MyProperty); // works

I am not exactly sure why the first option does not work but hopefully this helps get around it.

Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
  • How does that work and still end up binding to the model on form post? I am having that problem. The solution you presented works, but I lose the binding – Icestorm0141 May 21 '15 at 12:11
  • The first option doesn't work because these *For methods all call: `tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("name", fullHtmlFieldName, true);` The very last "true" parameter is "replaceAttributes", so it will overwrite values you supply in htmlAttributes with the fullHtmlFieldName value that it generates from your model property expression. As for "why" it was designed this way, I think your first paragraph hits the nail on the head... to ensure the right name is used to bind to your model property. – Triynko Jul 19 '16 at 04:12
1

Use TextBox instead of TextBoxFor

@Html.TextBox("MyPreferedName", Model.SearchParams.someParam)
alalmakt
  • 63
  • 5
0

TextBoxFor doesn't allow the name attribute to be set. This workaround:

@Html.EditorFor(m => m.UserName, null, "user_name")

outputs:

<input class="text-box single-line" id="user_name" name="user_name" type="text" value="" />
Fernando Correia
  • 21,803
  • 13
  • 83
  • 116
-1

Don't this work? @Html.TextBoxFor(m => m.SearchParams.someParam, new { id="my_id" }) although for your specific case it's be more like:

@Html.TextBoxFor(m => m.SearchParams.someParam, new { name = "MyPreferedName" })

Here you'll find the different overloads for the constructor of the InputExtensions.TextBoxFor in MVC3

PedroC88
  • 3,708
  • 7
  • 43
  • 77