2009-02-13 109 views
9

我有一個奇怪的問題,其中沒有顯示ValidationSummary。但是,正在顯示ValidationMessage。我已經檢查了輸出頁面的源代碼,並且不像它們以一種使它們模糊的顏色。我正在使用RC。有任何想法嗎?asp.net MVC - ValidationSummary不顯示

編輯:設定的ValidationSummary 斷點顯示:

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

確實的ValidationSummary使用的ErrorMessage和ValidationMessage使用InnerException.Message?

我的看法代碼:

<%@ 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> 

回答

6

你不說什麼的錯誤是不顯示,但也有其將顯示在ValidationMessage但不是在的ValidationSummary一些錯誤。我認爲這是發行候選人中的一個錯誤,但我願意接受其他解釋。特別是,從ValidationSummary源代碼考慮以下行:

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

請注意,沒有任何內容傳遞給modelState。現在,隨着ValidationMessage對比:

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

最後,讓我們來看看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); 
    } 

這告訴我們的是,如果你指定一個自定義錯誤消息,當您添加一個錯誤模型的狀態,它會被顯示。但是,如果添加了一個異常(AddModelError有一個接受異常的重載,另一個接受字符串;實現IDataErrorInfo的工作方式與字符串大小寫相同),而不是字符串錯誤消息,只有在modelState爲非null,然後我們給你一個通用的消息,而不是異常的錯誤消息。

更新

確實的ValidationSummary使用的ErrorMessage和ValidationMessage使用InnerException.Message?

是的,這或多或少的影響。就像我說的,我認爲這是一個錯誤。

UPDATE2

微軟更新GetUserErrorMessageOrDefault功能看到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); 
    } 
+0

嗨克雷格:對於PurchaseOrderId(整數)和日期(日期)我輸入的字符串。我試着用/不用自定義錯誤信息。我使用UpdateModel來獲取添加錯誤。我是一個nb,我只是想知道你在說什麼。我已經用更多信息更新了我的問題。 – 2009-02-13 14:36:27

+0

嗨我也認爲這是一個錯誤,因爲我的驗證總結不顯示所有的錯誤,但我的在線驗證信息。我認爲這個錯誤是在MVC2 RC2中引入的。 – Naz 2010-02-23 12:33:36

11

嘗試

<%= Html.ValidationSummary(false) %>