开发者

Adding more than one key for the same ModelState error

开发者 https://www.devze.com 2023-04-11 03:22 出处:网络
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.

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, t开发者_开发问答he "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 !


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");
}


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", "");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消