Show ValidationSummary MVC3 as “alert-error” Bootstrap

后端 未结 16 991
情书的邮戳
情书的邮戳 2021-01-30 00:51

I want to show a ValidationSummary mcv3 with \"alert-error\" Bootstrap styling.

I\'m using a Razor view, and I show model errors with this code:

 @Html.V         


        
16条回答
  •  天涯浪人
    2021-01-30 01:43

    Consider writing an extension method to the HtmlHelper like:

    public static class HtmlHelperExtensions
    {
        public static HtmlString ValidationSummaryBootstrap(this HtmlHelper htmlHelper)
        {
            if (htmlHelper == null)
            {
                throw new ArgumentNullException("htmlHelper");
            }
    
            if (htmlHelper.ViewData.ModelState.IsValid)
            {
                return new HtmlString(string.Empty);
            }
    
            return new HtmlString(
                "
    " + htmlHelper.ValidationSummary() + "
    "); } }

    Then you just need to fit the ul-li styling in your stylesheet.

提交回复
热议问题