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

前端 未结 11 1106
傲寒
傲寒 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:27

    Another way of doing it is to check if there are any li elements, if not just hide validation-summary-errors

    <script type="text/javascript">
         $(document).ready(function () {
              if ($(".validation-summary-errors li:visible").length === 0) {
                   $(".validation-summary-errors").hide();
              }
         });
    </script>
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-13 14:33

    @if (ViewContext.ViewData.ModelState.Count > 0)

    {

    //Your content

    }

    Would work like charm.

    0 讨论(0)
  • 2020-12-13 14:36

    Another variation of the fix with Bootstrap classes is:

    public static class ValidationSummaryExtensions
    {
        public static MvcHtmlString CleanValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors, string message = null)
        {
            if(htmlHelper == null) throw new ArgumentNullException("htmlHelper");
    
            MvcHtmlString validationSummary = null;
            if (htmlHelper.ViewData.ModelState.ContainsKey(string.Empty))
            {
                var htmlAttributes = new { @class = "alert alert-danger" };
                validationSummary = htmlHelper.ValidationSummary(excludePropertyErrors, message, htmlAttributes);
            }
    
            return validationSummary;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 14:39

    I know a workaround has been found but I had a similar problem with an easier solution.

    Is your validation summary rendering with css class "validation-summary-valid" or "validation-summary-errors"? The first class is applied with the validation summary is empty, the second is applied when it's populated.

    I've noticed that a placeholder div is rendered if the validation summary contains no errors so client side validation can display it if any fields fail validation.

    In the default MVC stylesheet 'Site.css' they just suppress displaying the empty validation summary with 'display:none;' e.g. .validation-summary-valid { display: none; }

    0 讨论(0)
提交回复
热议问题