ASP.NET MVC 3:覆盖“名称”属性与 TextBoxFor

发布于 2024-11-08 02:59:31 字数 375 浏览 0 评论 0原文

使用 Html.TextBoxFor 是否可以覆盖 name 属性?

我尝试过但没有成功。我需要使用 TextBoxFor 来使客户端验证正常工作,但是由于我不会详细说明的原因,我需要文本框的名称与生成的名称不同。

我尝试过以下方法:

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

适用于 ID,但不适用于名称。这可能吗?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

香橙ぽ 2024-11-15 02:59:31

罗布,实际上有一个更简单的方法。使用名称代替名称

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

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" })
放低过去 2024-11-15 02:59:31

您问这个问题是因为您想在名称中应用前缀吗?如果是这样,您可以通过在控制器中设置 ViewData.TemplateInfo.HtmlFieldPrefix 来完成此操作。

我从 布拉德·威尔逊的博客

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.

鹤仙姿 2024-11-15 02:59:31

EditorFor 有一个重载,您可以在其中提供 name 属性作为参数:

 @Html.EditorFor(expression, null, name)

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

 @Html.EditorFor(expression, null, name)
倾其所爱 2024-11-15 02:59:31

它被称为 Microsoft GOTCHA...

使用大写的名称,就像这样

@Html.TextBoxFor(m => m.Reply.Answer, new { Name = "Whatyouwant" })

It is called Microsoft GOTCHA...

Use the name in caps, like this

@Html.TextBoxFor(m => m.Reply.Answer, new { Name = "Whatyouwant" })
注定孤独终老 2024-11-15 02:59:31

尝试 EditorFor。如果您想确保即使属性类型不是字符串也呈现文本框,您可以传递字符串作为模板名称。如果属性已经是字符串,则不需要 templatename 显式渲染文本框,因此您可以传递 null。请注意,它不需要显式地要求 id 参数,它将从元素名称中推断出来。并且所有验证内容在 EditorFor 中仍然处于活动状态

 @Html.EditorFor(x => x.Data, "string", Model.Key + "_Data")

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")
蹲墙角沉默 2024-11-15 02:59:31

本的回答让我得到了我想要的东西,除了你需要用 Html.Raw 换行

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

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"))
无需解释 2024-11-15 02:59:31

有点“不漂亮”=),尝试:

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

a little bit "unpretty"=), try:

@Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData")
嘿咻 2024-11-15 02:59:31

对我来说,它有效!我希望有帮助!

@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control", @maxlength = "80", @id = "NomeFilter", @Name = "NomeFilter" } })

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" } })
兔小萌 2024-11-15 02:59:31
@Html.EditorFor(Model => Model.Something, "name", "name", new {@class = "form-control" })

不确定中间的两个字符串参数中的哪一个起作用,但只有当我输入这两个参数时它才起作用。

@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.

寒冷纷飞旳雪 2024-11-15 02:59:31

对于此示例,我根据权限禁用表单字段,但仍显示它们。我有一个隐藏字段来将值发送到控制器,但希望在 EditorFor 中使用不同的字段名称。
模型值后的第一个参数表示“名称”属性,第二个参数是新名称。

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

结果:

<input class="form-control text-box single-line" disabled="disabled" id="UserNameDisabled" name="UserNameDisabled" type="text" value="someEnteredValue" /> 

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" /> 
‘画卷フ 2024-11-15 02:59:31

保持简单,您已经提供了 ID,您应该能够使用方法“TextBox”而不是“TextBoxFor”,并且它将在客户端和服务器端正常工作。此外,虽然接受的答案可以工作,但如果您使用浏览器检查它,则会在标签上产生重复的名称属性。下面的解决方案不存在这个问题。

MvcHtmlString Html.TextBox(字符串名称,字符串值,对象htmlAttributes)

@Html.TextBox(Model.Key + "_Data", Model.Key, new { id = Model.Key + "_Data" }

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" }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文