如何延长在ValidationSummary HTML帮助中的ASP.NET MVC? [英] How to extend the ValidationSummary HTML Helper in ASP.NET MVC?

查看:81
本文介绍了如何延长在ValidationSummary HTML帮助中的ASP.NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要包装验证摘要中的 DIV 的。如何设置验证摘要与包装它的 DIV 的时候错误是present?

 < D​​IV CLASS =验证-摘要>
  <%= Html.ValidationSummary(登录失败,请更正错误,然后再试一次。)%>
< / DIV>


解决方案

我必须在我的另一个项目扩展验证摘要扩展来处理页面上的多个表单。

尽管这是不同的,你可以创建自己的扩展方法...

 命名空间System.Web.Mvc
{
    公共静态类ViewExtensions
    {
        公共静态字符串MyValidationSummary(这个HTML的HtmlHelper,串validationMessage)
        {
            如果(!html.ViewData.ModelState.IsValid)
            {
                返回< D​​IV CLASS = \\验证,总结\\>中+ html.ValidationSummary(validationMessage)+< / DIV>中
            }            返回;
        }
    }
}

然后就叫

 <%= Html.MyValidationSummary(
    登录失败,请更正错误,然后再试一次。)%>

HTHS,
查尔斯

I need to wrap the Validation Summary in a div. How do I set the Validation Summary to wrap it with a div when errors are present?

<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

这篇关于如何延长在ValidationSummary HTML帮助中的ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆