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

前端 未结 11 1115
傲寒
傲寒 2020-12-13 13:51

I am having a slight issue with the use of ValidationSummary(true) to display model level errors. If the ModelState does not contain model errors (i.e. M

11条回答
  •  一向
    一向 (楼主)
    2020-12-13 14:32

    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)
    

提交回复
热议问题