Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Validationsummary with excludePropertyErrors = true renders empty if the model is invalid

Say you have a standard ValidationSummary:

<%: Html.ValidationSummary(excludePropertyErrors: true) %>

If the ModelState contains model errors for properties but not for the model itself the ValidationSummary renders the following HTML:

<div class="validation-summary-errors"><ul><li style="display:none"></li></ul></div>

Which is displayed as an empty list but is still visible because of the red border around the list. This seems to be a bug to me. Can I turn off that the ValidationSummary helper will ever render an empty list?

like image 687
usr Avatar asked Oct 03 '10 11:10

usr


1 Answers

Similar to usr's answer, I fixed this with the following:

public static IHtmlString FixedValidationSummary(this HtmlHelper htmlHelper)
{
    return htmlHelper.FixedValidationSummary(false);
}

public static IHtmlString FixedValidationSummary(this HtmlHelper htmlHelper,
    bool excludePropertyErrors)
{
    var result = htmlHelper.ValidationSummary(excludePropertyErrors);
    return result == null || result.ToString().Contains("display:none") ? null : result;
}

This way I don't have to actually fork the original.

Agreed, though, this is very annoying.

like image 115
Anthony Mills Avatar answered Oct 20 '22 13:10

Anthony Mills