asp.net MVC - ValidationSummary 不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/545941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:07:41  来源:igfitidea点击:

asp.net MVC - ValidationSummary not displaying

.netasp.net-mvcvalidation

提问by Alan T

I have a strange problem whereby the ValidationSummary is not being displayed. However, the ValidationMessage's are being displayed. I've checked the output page source and it's not as though they are in a colour that is obscuring them. I'm using the RC. Any ideas?

我有一个奇怪的问题,即未显示 ValidationSummary。但是,正在显示 ValidationMessage。我已经检查了输出页面的源代码,并且它们的颜色并没有遮挡它们。我正在使用 RC。有任何想法吗?

Edit: Break point set at ValidationSummary shows:

编辑:在 ValidationSummary 设置的断点显示:

ViewData.ModelState.Values[1].ErrorMessage = ""
ViewData.ModelState.Values[1].Exception.InnerException.Message = "4a is not a valid value for Int32"

Does ValidationSummary use ErrorMessage and ValidationMessage use InnerException.Message?

ValidationSummary 使用 ErrorMessage 和 ValidationMessage 使用 InnerException.Message 吗?

My view code is:

我的视图代码是:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<App.Models.PurchaseOrdersView>" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title>Edit</title>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit</h2>

    <%= Html.ValidationSummary() %>

    <% Html.BeginForm("Edit", "PurchaseOrder", FormMethod.Post); %>
    <table>
        <tr>
            <td>
                Purchase Order Id:
            </td>
            <td>
                <%= Html.TextBox("PurchaseOrderId", Model.PurchaseOrderId)%>
                <%= Html.ValidationMessage("PurchaseOrderId")%>
            </td>
        </tr>
        <tr>
            <td>
                Date:
            </td>
            <td>
                <%= Html.TextBox("Date", Model.Date.ToString("dd-MMM-yyyy"))%>
                <%= Html.ValidationMessage("Date")%>
            </td>
        </tr>
    </table>
    <input type="submit" value="Save" />

    <% Html.EndForm(); %>

</asp:Content>

采纳答案by Craig Stuntz

You don't say what the errors are that are not displaying, but there are some errors which will be displayed in ValidationMessage but not in ValidationSummary. I think that this is a bug in the Release Candidate, but I am open to other interpretations. In particular, consider this line from the ValidationSummary source code:

您没有说明未显示的错误是什么,但有一些错误将显示在 ValidationMessage 中而不是 ValidationSummary 中。我认为这是候选版本中的一个错误,但我愿意接受其他解释。特别是,请考虑 ValidationSummary 源代码中的这一行:

string errorText = GetUserErrorMessageOrDefault(modelError, null /* modelState */);

Note that nothing is passed for modelState. Now contrast that with ValidationMessage:

请注意,modelState 没有传递任何内容。现在将其与 ValidationMessage 进行对比:

... GetUserErrorMessageOrDefault(modelError, modelState) ...

Finally, let's look at GetUserErrorMessageOrDefault:

最后,让我们看看 GetUserErrorMessageOrDefault:

    private static string GetUserErrorMessageOrDefault(ModelError error, ModelState modelState) {
        if (!String.IsNullOrEmpty(error.ErrorMessage)) {
            return error.ErrorMessage;
        }
        if (modelState == null) {
            return null;
        }

        string attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
        return String.Format(CultureInfo.CurrentCulture, MvcResources.Common_ValueNotValidForProperty, attemptedValue);
    }

What this tells us is that if you specify a custom error message when you add an error to the model state, it will be displayed. If, however, an exception is added (there is one overload for AddModelError which takes an exception, another which takes a string; implementing IDataErrorInfo works like the string case), instead of a string error message, it will only be displayed if modelState is non-null, and then we'll give you a generic message instead of the error message on the exception.

这告诉我们,如果在向模型状态添加错误时指定自定义错误消息,它将显示出来。但是,如果添加了异常(AddModelError 的一个重载接受异常,另一个重载接受字符串;实现 IDataErrorInfo 的工作方式类似于字符串大小写),而不是字符串错误消息,它只会在 modelState 为非空,然后我们会给你一个通用消息而不是异常的错误消息。

Update

更新

Does ValidationSummary use ErrorMessage and ValidationMessage use InnerException.Message?

ValidationSummary 使用 ErrorMessage 和 ValidationMessage 使用 InnerException.Message 吗?

Yes, that's more or less the effect. Like I said, I think this is a bug.

是的,这或多或少的效果。就像我说的,我认为这是一个错误。

Update2

更新2

Microsoft updated the GetUserErrorMessageOrDefault function as seen here.

微软更新GetUserErrorMessageOrDefault功能看到这里

private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
    {
        if (!String.IsNullOrEmpty(error.ErrorMessage))
        {
            return error.ErrorMessage;
        }
        if (modelState == null)
        {
            return null;
        }

        string attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
        return String.Format(CultureInfo.CurrentCulture, GetInvalidPropertyValueResource(httpContext), attemptedValue);
    }

回答by Dmitriy Radionov

Try

尝试

<%= Html.ValidationSummary(false) %>