Why is ValidationSummary(true) displaying an empty summary for property errors?

@if (ViewContext.ViewData.ModelState.Where(x => x.Key == "").Any())
{
    @Html.ValidationSummary(true, null, new { @class = "ui-state-error" })
}

This checks if there are any model wide errors and only renders the summary when there are some.


I think there is something wrong with the ValidationSummary helper method. You could easily create a custom helper method that wraps the built-in ValidationSummary.

public static MvcHtmlString CustomValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors)
{
  var htmlString = htmlHelper.ValidationSummary(excludePropertyErrors);

  if (htmlString != null)
  {
    XElement xEl = XElement.Parse(htmlString.ToHtmlString());

    var lis = xEl.Element("ul").Elements("li");

    if (lis.Count() == 1 && lis.First().Value == "")
      return null;
  }

  return htmlString;
}

Then from your view,

@Html.CustomValidationSummary(true)

Check this question too.

You could hide the summary with CSS:

.validation-summary-valid { display:none; }

Also, you could put the validation summary before the Html.BeginForm().