如何在 ASP.NET MVC 中扩展 ValidationSummary HTML Helper? [英] How to extend the ValidationSummary HTML Helper in ASP.NET MVC?

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

问题描述

我需要将验证摘要包装在 div 中.当出现错误时,如何设置验证摘要以使用 div 包装它?

<%= Html.ValidationSummary("登录失败.请更正错误并重试.") %>

解决方案

我不得不在我的另一个项目中扩展验证摘要扩展,以处理页面上的多个表单.

虽然这是不同的,但您可以创建自己的扩展方法...

命名空间 System.Web.Mvc{公共静态类 ViewExtensions{公共静态字符串 MyValidationSummary(this HtmlHelper html, string validationMessage){如果 (!html.ViewData.ModelState.IsValid){return "<div class="validation-summary">"+ html.ValidationSummary(validationMessage) + "</div>"}返回 "";}}}

然后就打电话

<%= Html.MyValidationSummary(登录失败.请更正错误并重试.") %>

HTH,查尔斯

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

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

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