在 MVC 2 中使用 DataAnnotations 时,是否有一种显示所需字段指示符的好方法?
我已经在所有模型上使用 DataAnnotations 进行了验证,但我想在页面加载时显示必填字段的指示器。由于我已经集中了所有验证,因此我不想在视图中硬编码指示器。在加载时调用验证将显示验证摘要。有没有人找到一种好方法让模型确定需要什么,但在渲染视图时检查它,类似于 Html.ValidationMessageFor?
I've got validation working with DataAnnotations on all my models, but I'd like to display an indicator for required fields on page load. Since I've got all my validation centralized, I'd rather not hard-code indicators in the View. Calling validation on load would show the validation summary. Has anyone found a good way of letting the model determine what's required, but checking it upon rendering the view, similar to Html.ValidationMessageFor
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这超出了我的想象,但它应该可以帮助您开始:
This is off the top of my head, but it should get you started:
您可以添加一个渲染方法,该方法使用反射来检查字段上的必需属性。
You could add a render method that uses reflection to check for the Required attribute on the field.