将错误消息添加到@Html.ValidationSummary
我使用标准 MVC3 Razor 视图和不显眼的 Javascript 验证,并使用 @Html.ValidationSummary
将它们显示在表单顶部。如果标准验证(例如 [Required]
)通过,我就会运行一些非常自定义的客户端验证,当用户点击“提交”按钮时会触发该验证。 (验证会检查多个表单元素,以确保检查了正确的一组表单元素等,因此它并不像为单个字段创建新的自定义验证器那么简单)。
我希望我在那里构建的可能错误显示在 ValidationSummary
列表中,但我不知道如何让错误消息出现在那里。
I'm using standard MVC3 Razor views with unobtrusive Javascript validation, using @Html.ValidationSummary
to show them at the top of the form. If the standard validations (things like [Required]
) pass, I then run some very custom client-side validation that fires when the user hits the Submit button. (The validation looks across a number of form elements to make sure that the proper set of them have been checked, etc., so it's not as simple as just creating a new custom validator for a single field).
I'd like the possible error(s) I construct there to be shown in the ValidationSummary
list, but I can't figure out how to get the error message to appear there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在客户端:
或在服务器端:
认为您有一个这样的模型:
当您验证它时:
In client-side:
OR In server-side:
Think you have a model like this:
and when you are validating it:
您只需将
Error Message
添加到ModelState
即可,只要您调用了ValidationSummary()
即可显示错误消息你的观点。要将错误添加到
ModelState
只需执行以下操作:You can do so just adding the
Error Message
to theModelState
should display the error message for you, provided that you haveValidationSummary()
called on your view.To add the error to the
ModelState
just do this: