How to extend the HTML Helper ValidationSummary in ASP.NET MVC?

I need to wrap a check summary in a div. How to set a check summary to wrap it with a div if there are errors?

<div class="validation-summary"> <%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %> </div> 
+14
validation asp.net-mvc
May 28 '09 at 2:40
source share
4 answers

I had to expand consolidated validation extensions in another project of mine to deal with more than one form per page.

Although this is different, you can create your own extension method ...

 namespace System.Web.Mvc { public static class ViewExtensions { public static string MyValidationSummary(this HtmlHelper html, string validationMessage) { if (!html.ViewData.ModelState.IsValid) { return "<div class=\"validation-summary\">" + html.ValidationSummary(validationMessage) + "</div>" } return ""; } } } 

Then just call

 <%= Html.MyValidationSummary( "Login was unsuccessful. Please correct the errors and try again.") %> 

HTHS, Charles

+24
May 28 '09 at 5:19
source share

What you can do is:

 <%if (!ViewData.ModelState.IsValid) { %> <div class="validation-summary"> <%= Html.ValidationSummary( "Login was unsuccessful. Please correct the errors and try again.") %> </div> <% } %> 
+12
May 28 '09 at 4:34
source share

For MVC 2, ValidationSummary is an extension method, you must add

 using System.Web.Mvc.Html; 
+5
Sep 09 '10 at 21:53
source share

Use this CSS tag for li, for example ...

 .validation-summary-errors ul li {color:Red;} 
0
Jun 11 2018-11-11T00:
source share



All Articles