12

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

enter image description here

enter image description here

g.pickardou
  • 32,346
  • 36
  • 123
  • 268

2 Answers2

13

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.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

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

Abbas Galiyakotwala
  • 2,949
  • 4
  • 19
  • 34