'Adding more than one key for the same ModelState error

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 !



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mmcfly