NET MVC 3: 用 TextBoxFor 覆盖“ name”属性

在使用 Html.TextBoxFor覆盖 name 属性时是否可能?

我试过了,但没有成功。我需要使用 TextBoxFor 来使客户端验证工作,但是由于某些原因,我需要文本框的名称与生成的名称不同。

我试过以下方法:

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

只能用 ID,不能用名字,这可能吗?

更新 : 查看 TextBoxFor 的代码。看起来没有简单的办法。希望有人能证明我错了。

81639 次浏览

a little bit "unpretty"=), try:

@Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData")

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")

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.

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"))

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" })

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

 @Html.EditorFor(expression, null, name)

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" } })

It is called Microsoft GOTCHA...

Use the name in caps, like this

@Html.TextBoxFor(m => m.Reply.Answer, new { Name = "Whatyouwant" })
@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.

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

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" }