为 ASP.NET MVC 2.0 中的视图中的 [Required] 字段定义标记
我们有一个模型,其属性用 [Required] 装饰,非常适合验证。但是,我们想要做的是在视图中用星号(或其他样式)标记这些必填字段,以表示在用户输入任何数据进行验证之前它们是必需的。
我似乎无法找到 MVC 框架中内置的任何内容来允许我执行此操作,因此想知道是否有其他人已经解决了这个问题以及如何解决?
最终,我们试图防止的是,如果我们随后从模型属性中删除 [Required],则需要在两个位置更改代码。
非常感谢
We have a model with properties decorated with [Required] which works great for validation. However, what we'd like to do is mark those required fields in the view with an asterisk (or some other style) to denote they are required before the user enters any data for validation.
I don't seem to be able to find anything built into the MVC framework to allow me to do this and therefore wondered if anyone else had already solved this and how?
Ultimately what we're trying to prevent is the need to change code in two places if we subsequently remove a [Required] from a model property.
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以为此构建自己的 HtmlHelper 扩展:
You can build your own HtmlHelper extension for this:
Gregoire 和 Faxanadu 的解决方案的问题是,当使用正常验证对字段进行验证并且使用“Html.ValidationMessageFor()”时,始终显示“*”。
因此,如果您使用这样的 html 扩展:
您将看到重复的 *,以防字段出现验证错误。
我基于 this 它还会检查该字段是否已验证。
The problem with the solution from Gregoire and Faxanadu is that the "*" is always displayed, also when the field is validated using the normal validation and you use 'Html.ValidationMessageFor()'.
So if you use the html extension like this:
You'll see duplicate * in case the field has validation errors.
I've made this modified extension, based on this which does also check if the field was validated.
如果您在使用“ModelMetadata.FromLambdaExpression”时收到“无效参数”错误,请确保为 HtmlHelper 指定通用类型参数。这是格雷瓜尔解决方案的修订版本:
If you are getting an "invalid arguments" error when using "ModelMetadata.FromLambdaExpression", make sure you specify a generic type parameter for HtmlHelper. Here is a revised version of Gregoire's solution:
如果您使用完整表单模板化帮助程序 EditorFor 和 DisplayFor,您需要自定义您自己的 Object.ascx 版本。
Brad Wilson 有一篇很棒的博客文章,概述了这一点:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
会发生什么如果 ModelMetadata 上的 IsRequired 属性将为 true。 Object.ascx 内部:
如果您不使用模板化帮助程序,则必须编写自己的 html 帮助程序。您的观点如何?
If you are using full form templated helpers EditorFor and DisplayFor you need to customize your own version of Object.ascx.
Brad Wilson has an awesome blog post which outlines this:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
What happens it the IsRequired property on ModelMetadata will be true. Inside Object.ascx:
If your not using templated helpers you'd have to write your own html helper. What do your views look like?
我现在的问题有点老了,但我正在努力使用 jquery 找到一个不显眼的解决方案。
我的目标是获取所有标有 data-val-required 属性的输入,然后设置其标签元素标记为 for
以下是示例 jquery 代码:
I now the question is a little old, but I am working to an unobstrusive solution using just jquery.
My goal is to get all inputs marked with data-val-required attribute, and then set its label elementes markeds with attribute for
Here is the sample jquery code: