We have a model that is being displayed in a form on a partial view with model validations. We have a few fields like this:
[Required(ErrorMessage = "{0} cannot be empty")]
public string FirstName { get; set; }
[Required(ErrorMessage = "{0} cannot be empty")]
public string LastName { get; set; }
[RegularExpression(@"(\d{5})?", ErrorMessage = "Zip must be a 5 digit number or empty.")]
public string ZipCode { get; set; }
Then in the view we are displaying text boxes for those fields with validation messages for them like this:
<td>
@Html.TextBoxFor(m => m.Officer.FirstName)
@Html.ValidationMessageFor(m => m.Officer.FirstName)
</td>
<td>
@Html.TextBoxFor(m => m.Officer.LastName)
@Html.ValidationMessageFor(m => m.Officer.LastName)
</td>
<td>
@Html.TextBoxFor(m => m.Officer.ZipCode)
@Html.ValidationMessageFor(m => m.Officer.ZipCode)
</td>
The validations are working, but I am noticing something odd with the timing and I haven't been able to find any documentation as to why.
When filling out the form, if I leave FirstName or LastName blank, the validation messages don't appear until I submit the form, check if the model state is valid and return to the page.
However, if I edit zip code with an invalid entry, the validation message popups immeadiately, even if I haven't submitted the form.
Is there anyway to get them to process at the same time so that it doesn't appear weird to the user?