简体   繁体   English

如何在ASP.NET MVC中扩展ValidationSummary HTML Helper?

[英]How to extend the ValidationSummary HTML Helper in ASP.NET MVC?

I need to wrap the Validation Summary in a div . 我需要将验证摘要包装在div中 How do I set the Validation Summary to wrap it with a div when errors are present? 当出现错误时,如何设置验证摘要以将其包装为div

<div class="validation-summary"> 
  <%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
</div>

I had to extend the validation summary extensions in another project of mine to deal with more than one form on a page. 我不得不在我的另一个项目中扩展验证摘要扩展,以处理页面上的多个表单。

Although this is different, you could create your own extension method... 虽然这有所不同,但您可以创建自己的扩展方法......

namespace System.Web.Mvc
{
    public static class ViewExtensions
    {
        public static string MyValidationSummary(this HtmlHelper html, string validationMessage)
        {
            if (!html.ViewData.ModelState.IsValid)
            {
                return "<div class=\"validation-summary\">" + html.ValidationSummary(validationMessage) + "</div>"
            }

            return "";
        }
    }
}

Then just call 然后打电话

<%= Html.MyValidationSummary(
    "Login was unsuccessful. Please correct the errors and try again.") %>

HTHs, Charles HTHs,查尔斯

What you can do is this : 你能做的是:

<%if (!ViewData.ModelState.IsValid) { %>
<div class="validation-summary"> 
    <%= Html.ValidationSummary(
        "Login was unsuccessful. Please correct the errors and try again.") %>
</div>
<% } %>

对于MVC 2,ValidationSummary是一种扩展方法,您必须添加

using System.Web.Mvc.Html;

例如,将此CSS用于li标签...

.validation-summary-errors ul li {color:Red;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM