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

3
  • 1
    @Html.ValidationSummary(true)
    – user3559349
    Apr 24, 2015 at 12:18
  • @Stephen I initially thought this would've been answered before. Couldn't find a dupe though. Do you know of any?
    – CodeCaster
    Apr 24, 2015 at 12:21
  • @CodeCaster I think I answered an almost identical question before - just can't find it yet
    – user3559349
    Apr 24, 2015 at 12:25

2 Answers 2

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.

1
  • Perfect answer! Even answered a follow-up question that usually arises :)
    – Kunal
    Dec 6, 2018 at 16:58
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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.