为同一 ModelState 错误添加多个键
我一直在寻找这个解决方案一段时间,我想问你最好的方法是什么。
假设我有两个字段填写了日期,而该期间无效。
发现这一点后,我需要向用户发送错误,并需要突出显示与此错误相关的字段。
if((secondDate.Value - firstDate.Value).Days > 31)
{
ModelState.AddModelError("firstDate", "The period must contains less than 31 days");
}
这样,“firstDate”字段就可以很好地工作,我想让“secondDate”字段具有相同的行为。
是否可以?哪一个是最好的呢?
谢谢 !
I've been looking for this solution for a while and I'd like to ask you what's the best way to do that.
Suppous that I have two fields filled up with a date and this period is invalid.
After discovering this I need to send the user an error and need to highlight the field related to this error.
if((secondDate.Value - firstDate.Value).Days > 31)
{
ModelState.AddModelError("firstDate", "The period must contains less than 31 days");
}
With this, the "firstDate" field works nicelly and I would like to make the "secondDate" field have the same behave.
Is it possible? Wich is the best what for that?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我目前使用的是 MVC 5.2.3.0。如果您使用验证摘要并将 errorMessage 留空,它仍然会将错误 html 类添加到输入(在 UI 中将它们变为红色),但不会在验证摘要中添加第二条错误消息:
I'm currently on MVC 5.2.3.0. If you're using a validation summary and you leave the errorMessage blank, it will still add the error html classes to the input (turn them red in the UI), but it won't add a second error message in the validation summary: