ASP.NET MVC 3:覆盖“名称”属性与 TextBoxFor
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
罗布,实际上有一个更简单的方法。使用名称代替名称:
Rob, actually there is a much simpler way. Instead of name, use Name:
您问这个问题是因为您想在名称中应用前缀吗?如果是这样,您可以通过在控制器中设置 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.
EditorFor 有一个重载,您可以在其中提供
name
属性作为参数:EditorFor has an overload where you can supply the
name
attribute as a parameter:它被称为 Microsoft GOTCHA...
使用大写的名称,就像这样
It is called Microsoft GOTCHA...
Use the name in caps, like this
尝试 EditorFor。如果您想确保即使属性类型不是字符串也呈现文本框,您可以传递字符串作为模板名称。如果属性已经是字符串,则不需要
templatename
显式渲染文本框,因此您可以传递null
。请注意,它不需要显式地要求 id 参数,它将从元素名称中推断出来。并且所有验证内容在 EditorFor 中仍然处于活动状态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 passnull
. 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.Raw 换行
ben's answer got me what I was looking for except you need to wrap in in Html.Raw
有点“不漂亮”=),尝试:
a little bit "unpretty"=), try:
对我来说,它有效!我希望有帮助!
For me, it works! I hope that help!
不确定中间的两个字符串参数中的哪一个起作用,但只有当我输入这两个参数时它才起作用。
Not sure which of those two string parameters in the middle do the work, but it worked only when I typed both of them.
对于此示例,我根据权限禁用表单字段,但仍显示它们。我有一个隐藏字段来将值发送到控制器,但希望在 EditorFor 中使用不同的字段名称。
模型值后的第一个参数表示“名称”属性,第二个参数是新名称。
结果:
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.
Results in:
保持简单,您已经提供了 ID,您应该能够使用方法“TextBox”而不是“TextBoxFor”,并且它将在客户端和服务器端正常工作。此外,虽然接受的答案可以工作,但如果您使用浏览器检查它,则会在标签上产生重复的名称属性。下面的解决方案不存在这个问题。
MvcHtmlString Html.TextBox(字符串名称,字符串值,对象htmlAttributes)
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)