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

前端 未结 11 1137
傲寒
傲寒 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: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;
        }
    }
    

提交回复
热议问题