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.

2
  • Duplicate of stackoverflow.com/questions/6064363/… and stackoverflow.com/questions/6057865/… Commented Aug 20, 2012 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. Commented Jul 19, 2016 at 4:21

5 Answers 5

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

Just Use "Name" instead of "name"

Sign up to request clarification or add additional context in comments.

4 Comments

Why does that work!! Oh well...this does work as long as the name property is "Name"
This doesn't stop TextBoxFor from outputting the name attribute... this just adds another attribute named Name as well... not what was requested.
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.
In ASP.NET 5 this renders as <input type="text" name="attr" Name="txt1" /> unfortunately.
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.

2 Comments

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
The first option doesn't work because these *For methods all call: tagBuilder.MergeAttributes<string, object>(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.
1

Use TextBox instead of TextBoxFor

@Html.TextBox("MyPreferedName", Model.SearchParams.someParam)

Comments

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="" />

2 Comments

but using EditorFor does not allow me to set its htmlAttributes?
i use TextBox instead for my purpose.
-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

3 Comments

How about this: @Html.TextBoxFor(m => m.SearchParams.someParam, new { @name = "MyPreferedName" })
I actually did it with the @ when testing it but I am going to try it one more time.
I tried PedroC88's suggestion "@Html.TextBoxFor(m => m.SearchParams.someParam, new { @name = "MyPreferedName" })" and it didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.