I would not like to display the ValidationSummary in case it only displays already displayed field related errors. However I do need ValidationSummary when custom server side validation error occurs like:
if (!UserManager.IsEmailConfirmed(user.Id))
{
AuthenticationManager.SignOut();
ModelState.AddModelError("", "You need to confirm your email.");
return View(model);
}
Use @Html.ValidationSummary(excludePropertyErrors: true)
.
This overload, when excludePropertyErrors
is true
, hides property errors like your "The Email field is not a valid e-mail address." and "The Password field is required." from the validation summary. See also @Html.ValidationSummary(true) - What's the true do?.
It doesn't detect whether you print those though @Html.ValidationMessageFor()
, so if you forget any of those, you can get failing form submissions that don't tell you why they fail.
To manually add non-property validation errors, call ModelState.AddModelError("", "Custom error")
(note the empty string) as explained in Add error message to @Html.ValidationSummary and ASP.NET MVC Html.ValidationSummary(true) does not display model errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With