113

Is it possible when using Html.TextBoxFor to override the name attribute?

I have tried with no success. I need to use TextBoxFor to get client side validation to work, however for reasons I won't go into I need the name of the textbox to be different from the generated one.

I have tried the following:

@Html.TextBoxFor(x => x.Data, new { name = Model.Key + "_Data", id = Model.Key + "_Data" })

Which works for ID but not name. Is this possible?

Update: Looking into the code for TextBoxFor. It doesn't look like there is an easy way. Hopefully someone can prove me wrong.

Rob Stevenson-Leggett
  • 35,279
  • 21
  • 87
  • 141

11 Answers11

257

Rob, actually there is a much simpler way. Instead of name, use Name:

@Html.TextBoxFor(x => x.Data, new { Name = Model.Key + "_Data", id = Model.Key + "_Data" })
anar khalilov
  • 16,993
  • 9
  • 47
  • 62
43

Are you asking this because you want to apply a prefix to the name? If so, you can do this by setting ViewData.TemplateInfo.HtmlFieldPrefix in your Controller.

I learnt a lot about this stuff from Brad Wilson's blog.

James McCormack
  • 9,217
  • 3
  • 47
  • 57
14

EditorFor has an overload where you can supply the name attribute as a parameter:

 @Html.EditorFor(expression, null, name)
Protector one
  • 6,926
  • 5
  • 62
  • 86
  • 1
    Would have saved me hours if I found this hours ago, haha – Johnie Karr May 05 '16 at 01:16
  • Interesting, but there is no overload for the name attribute for similar helpers like DropDownListFor, you have to specify it in the html attributes parameter (with a capital N in Name). – nmit026 May 10 '19 at 00:43
9

It is called Microsoft GOTCHA...

Use the name in caps, like this

@Html.TextBoxFor(m => m.Reply.Answer, new { Name = "Whatyouwant" })
pogosama
  • 1,818
  • 1
  • 24
  • 29
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
  • It really makes the difference, if you use @name, it will not get overridden. Very hard to figure out. Thanks – GELR Sep 28 '21 at 12:20
8

Try EditorFor. you can pass string as template name if you want to make sure textbox is rendered even if property type is not string. If property is string already, it does not need templatename explicitly to render textbox, so you can pass null. Note that it does not require id parameter explicitly, it will infer it from element name. And all the validation things are still active with EditorFor

 @Html.EditorFor(x => x.Data, "string", Model.Key + "_Data")
archil
  • 39,013
  • 7
  • 65
  • 82
7

ben's answer got me what I was looking for except you need to wrap in in Html.Raw

@Html.Raw(Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData"))
Rip Ryness
  • 651
  • 1
  • 7
  • 14
4

a little bit "unpretty"=), try:

@Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData")
benwasd
  • 1,342
  • 10
  • 18
2

For me, it works! I hope that help!

@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control", @maxlength = "80", @id = "NomeFilter", @Name = "NomeFilter" } })
Jonas
  • 49
  • 3
1
@Html.EditorFor(Model => Model.Something, "name", "name", new {@class = "form-control" })

Not sure which of those two string parameters in the middle do the work, but it worked only when I typed both of them.

Mr.Ajdos
  • 11
  • 1
  • It's Id and Name. this worked for me. I went through all above answers and non of them worked, but this 1. Thanks. – Orion Jun 13 '18 at 03:46
  • `public static MvcHtmlString EditorFor(this HtmlHelper html, Expression> expression, string templateName, string htmlFieldName, object additionalViewData);`. No idea what `templateName` is supposed to be, so I used `null`. For `htmlFieldName` I used the name of the parameter (in my case "ipmi" instead of "IPMI"). That did the trick for me. – BCdotWEB Jun 18 '18 at 09:47
1

For this example, I was disabling form fields based on permissions, but still showing them. I had a hidden field to send the value to the controller, but wanted a different field name in the EditorFor. First param after model value represents the "name" property, second is the new name.

@Html.EditorFor(m => m.UserName, "name", "UserNameDisabled", new { htmlAttributes = new { @class = "form-control", @disabled = "disabled"} });

Results in:

<input class="form-control text-box single-line" disabled="disabled" id="UserNameDisabled" name="UserNameDisabled" type="text" value="someEnteredValue" /> 
ScottLenart
  • 1,160
  • 1
  • 12
  • 15
0

Keep it simple, your already providing the ID you should simply be able to use the method "TextBox" instead of "TextBoxFor" and it will work fine client side and server side. In addition, although the accepted answer will work but will produce duplicate Name attributes on your tag if you inspect it using a browser. The below solution does not have that problem.

MvcHtmlString Html.TextBox(string name, string value, object htmlAttributes)

@Html.TextBox(Model.Key + "_Data", Model.Key, new { id = Model.Key + "_Data" }
TroySteven
  • 4,885
  • 4
  • 32
  • 50