asp.net MVC - 的ValidationSummary不显示 [英] asp.net MVC - ValidationSummary not displaying

查看:638
本文介绍了asp.net MVC - 的ValidationSummary不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,从而不被显示在ValidationSummary。然而,所显示的ValidationMessage的。我检查了输出页面的源代码,它不是,虽然他们是在被掩盖它们的颜色。我使用的是钢筋混凝土。任何想法?

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

  ViewData.ModelState.Values​​ [1] .ErrorMessage =
ViewData.ModelState.Values​​ [1] .Exception.InnerException.Message =4a是不适合的Int32一个有效值
 

有没有的ValidationSummary使用的ErrorMessage和ValidationMessage使用InnerException.Message?

我的看法code是:

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/的Site.Master
继承=System.Web.Mvc.ViewPage< App.Models.PurchaseOrdersView>中%>


< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>
    <标题>编辑< /标题>
< / ASP:内容>

< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>

    < H2>编辑< / H>

    &其中;%= Html.ValidationSummary()%>

    <%Html.BeginForm(编辑,的PurchaseOrder,FormMethod.Post); %>
    <表>
        &其中; TR>
            < TD>
                采购订单编号:
            < / TD>
            < TD>
                &其中;%= Html.TextBox(PurchaseOrderId,Model.PurchaseOrderId)%>
                &其中;%= Html.ValidationMessage(PurchaseOrderId)%>
            < / TD>
        < / TR>
        &其中; TR>
            < TD>
                日期:
            < / TD>
            < TD>
                <%= Html.TextBox(日期,Model.Date.ToString(DD-MMM-YYYY))%>
                <%= Html.ValidationMessage(日期)%>
            < / TD>
        < / TR>
    < /表>
    <输入类型=提交值=保存/>

    &所述;%Html.EndForm(); %>

< / ASP:内容>
 

解决方案

你不说的错误是什么都没有显示,但也有一些错误,这将在的ValidationSummary显示在ValidationMessage,但并非如此。我认为这是在候选发布版中的错误,但我愿意接受其他跨pretations。特别是,考虑这条线从在ValidationSummary源$ C ​​$ C:

 字符串ERRORTEXT = GetUserErrorMessageOrDefault(modelError,空/ * ModelState中* /);
 

请注意,没有什么是过去了的ModelState。现在对比,与ValidationMessage:

  ... GetUserErrorMessageOrDefault(modelError,ModelState中)...
 

最后,让我们来看看GetUserErrorMessageOrDefault:

 私有静态字符串GetUserErrorMessageOrDefault(ModelError错误,ModelState中的ModelState){
        如果(!String.IsNullOrEmpty(error.ErrorMessage)){
            返回error.ErrorMessage;
        }
        如果(ModelState中== NULL){
            返回null;
        }

        字符串attemptedValue =(modelState.Value!= NULL)? modelState.Value.AttemptedValue:空;
        返回的String.Format(CultureInfo.CurrentCulture,MvcResources.Common_ValueNotValidForProperty,attemptedValue);
    }
 

这告诉我们的是,如果你指定一个自定义错误消息,当您添加一个错误模型的状态,它会显示。但是,如果有异常增加(有一个过载AddModelError这需要一个例外,另一个则需要一个字符串;实施IDataErrorInfo就像串的情况下),而不是一个字符串错误信息,这将只有当ModelState中显示非空,然后我们会给上,而不是异常的错误消息,一个通用的消息。

更新

  

有没有的ValidationSummary使用的ErrorMessage和ValidationMessage使用InnerException.Message?

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

UPDATE2

微软更新GetUserErrorMessageOrDefault功能如<一个href="http://aspnetwebstack.$c$cplex.com/SourceControl/changeset/view/f1511797ea32#src%2fSystem.Web.Mvc%2fHtml%2fValidationExtensions.cs"相对=nofollow>这里。

 私有静态字符串GetUserErrorMessageOrDefault(HttpContextBase HttpContext的,ModelError错误,ModelState中的ModelState)
    {
        如果(!String.IsNullOrEmpty(error.ErrorMessage))
        {
            返回error.ErrorMessage;
        }
        如果(ModelState中== NULL)
        {
            返回null;
        }

        字符串attemptedValue =(modelState.Value!= NULL)? modelState.Value.AttemptedValue:空;
        返回的String.Format(CultureInfo.CurrentCulture,GetInvalidPropertyValueResource(HttpContext的),attemptedValue);
    }
 

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?

Edit: Break point set at ValidationSummary shows:

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?

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>

解决方案

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:

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

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

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

Finally, let's look at 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.

Update

Does ValidationSummary use ErrorMessage and ValidationMessage use InnerException.Message?

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

Update2

Microsoft updated the GetUserErrorMessageOrDefault function as seen here.

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

这篇关于asp.net MVC - 的ValidationSummary不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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