为同一 ModelState 错误添加多个键

发布于 2024-12-08 11:16:21 字数 377 浏览 0 评论 0原文

我一直在寻找这个解决方案一段时间,我想问你最好的方法是什么。

假设我有两个字段填写了日期,而该期间无效。

发现这一点后,我需要向用户发送错误,并需要突出显示与此错误相关的字段。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太傻旳人生 2024-12-15 11:16:21
if((firstDate.Value - secondDate.Value).Days > 31)
{
    ModelState.AddModelError("firstDate", "The period must contains less than 31 days");
    ModelState.AddModelError("secondDate", "The period must contains less than 31 days");
}
if((firstDate.Value - secondDate.Value).Days > 31)
{
    ModelState.AddModelError("firstDate", "The period must contains less than 31 days");
    ModelState.AddModelError("secondDate", "The period must contains less than 31 days");
}
箹锭⒈辈孓 2024-12-15 11:16:21

我目前使用的是 MVC 5.2.3.0。如果您使用验证摘要并将 errorMessage 留空,它仍然会将错误 html 类添加到输入(在 UI 中将它们变为红色),但不会在验证摘要中添加第二条错误消息:

if((secondDate.Value - firstDate.Value).Days > 31)
{
    ModelState.AddModelError("firstDate", "The period must contains less than 31 days");
    ModelState.AddModelError("secondDate", "");
}

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:

if((secondDate.Value - firstDate.Value).Days > 31)
{
    ModelState.AddModelError("firstDate", "The period must contains less than 31 days");
    ModelState.AddModelError("secondDate", "");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文