Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not display ASP MVC ValidationSummary in case when errors already displayed field by field?

Tags:

c#

asp.net-mvc

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

like image 448
g.pickardou Avatar asked Apr 24 '15 12:04

g.pickardou


1 Answers

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.

like image 78
CodeCaster Avatar answered Nov 12 '22 04:11

CodeCaster