如果模型无效,带有 exceptPropertyErrors = true 的 ASP.NET MVC Validationsummary 将呈现为空

发布于 2024-09-25 21:12:55 字数 437 浏览 2 评论 0原文

假设您有一个标准的 ValidationSummary:

<%: Html.ValidationSummary(excludePropertyErrors: true) %>

如果 ModelState 包含属性的模型错误,但不包含模型本身的模型错误,则 ValidationSummary 会呈现以下 HTML:

<div class="validation-summary-errors"><ul><li style="display:none"></li></ul></div>

显示为空列表,但由于列表周围有红色边框,因此仍然可见。这对我来说似乎是一个错误。我可以关闭 ValidationSummary 帮助器呈现空列表的功能吗?

Say you have a standard ValidationSummary:

<%: Html.ValidationSummary(excludePropertyErrors: true) %>

If the ModelState contains model errors for properties but not for the model itself the ValidationSummary renders the following HTML:

<div class="validation-summary-errors"><ul><li style="display:none"></li></ul></div>

Which is displayed as an empty list but is still visible because of the red border around the list. This seems to be a bug to me. Can I turn off that the ValidationSummary helper will ever render an empty list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北音执念 2024-10-02 21:12:55

与 usr 的答案类似,我用以下方法修复了这个问题:

public static IHtmlString FixedValidationSummary(this HtmlHelper htmlHelper)
{
    return htmlHelper.FixedValidationSummary(false);
}

public static IHtmlString FixedValidationSummary(this HtmlHelper htmlHelper,
    bool excludePropertyErrors)
{
    var result = htmlHelper.ValidationSummary(excludePropertyErrors);
    return result == null || result.ToString().Contains("display:none") ? null : result;
}

这样我就不必实际分叉原始版本。

不过,同意,这很烦人。

Similar to usr's answer, I fixed this with the following:

public static IHtmlString FixedValidationSummary(this HtmlHelper htmlHelper)
{
    return htmlHelper.FixedValidationSummary(false);
}

public static IHtmlString FixedValidationSummary(this HtmlHelper htmlHelper,
    bool excludePropertyErrors)
{
    var result = htmlHelper.ValidationSummary(excludePropertyErrors);
    return result == null || result.ToString().Contains("display:none") ? null : result;
}

This way I don't have to actually fork the original.

Agreed, though, this is very annoying.

南风起 2024-10-02 21:12:55

当我通过查看源代码发现没有解决方案后,我通过 fork MVC 版本的代码并修改一行代码解决了问题。

After I found out that there is no solution by looking at the source I solved the problem by forking the MVC version of the code and modifying one line.

反目相谮 2024-10-02 21:12:55

Bootstrap 类修复的另一个变体是:

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;
    }
}

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;
    }
}
变身佩奇 2024-10-02 21:12:55

另一种解决方法是将样式移至 div。 CSS 如下:

div.validation-summary-valid {
    display: none;
}

当摘要返回且没有错误时,div 不会显示。当出现错误时,该类会自动更改为validation-summary-errors。

Another workaround is to move the style to the div. Here's the CSS:

div.validation-summary-valid {
    display: none;
}

When the summary returns with no errors the div is not displayed. When there are errors the class is automatically changed to validation-summary-errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文