Reputation: 35822
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);
}
Upvotes: 12
Views: 4787
Reputation: 151586
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.
Upvotes: 13
Reputation: 3019
like @CodeCaster say Simple add
@Html.ValidationSummary(true).
If the bool parameter is true then only model-level errors are displayed. If the parameter is false then all errors are shown. reference
Upvotes: 0