我用了很多時間來解決我的問題stackoverflow(代碼相關!)但這是我第一次需要發佈一個問題,因爲我無法工作怎麼了。客戶端驗證列表在ASP.Net MVC 2
當我能夠在一個視圖,允許其使用DataAnnotations進行驗證對象的集合,以下異常引發的編輯客戶端驗證:
[KeyNotFoundException: The given key was not present in the dictionary.]
System.Collections.Generic.SortedDictionary`2.get_Item(TKey key) +6129977
System.Web.Mvc.Html.ValidationExtensions.ValidationMessageHelper(HtmlHelper htmlHelper, ModelMetadata modelMetadata, String expression, String validationMessage, IDictionary`2 htmlAttributes) +840
System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression, String validationMessage, IDictionary`2 htmlAttributes) +138
System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression) +106
ASP.views_test_test_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\SVN\Discover2 - trunk\Discover2.Web\Views\Test\Test.aspx:18
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +56
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060
Line 18: <%: Html.ValidationMessageFor(m => Model[i].Name)%>
如果我刪除<% Html.EnableClientValidation(); %>
通話不發生異常和服務器端驗證按預期工作。
這裏是我的測試型號:
public class Dog {
[Required]
public string Name { get; set; }
}
和看法:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IList<Discover2.Web.Controllers.Dog>>" %>
<% Html.EnableClientValidation(); %>
<%using (Html.BeginForm()) { %>
<%for (int i = 0; i < Model.Count(); i++) {%>
<div>
<%: Html.LabelFor(m => Model[i].Name) %>
<%: Html.TextBoxFor(m => Model[i].Name)%>
<%: Html.ValidationMessageFor(m => Model[i].Name)%>
</div>
<%} %>
<button type=submit>Save</button>
如何得到這個工作的任何想法,將不勝感激其一直在做我的頭,試圖讓這工作!
感謝
謝謝你,但沒有喜樂。在你的建議重新命名後,我嘗試將該屬性的名稱更改爲DogsName,但這也不起作用。然後嘗試Html.ValidationMessage(String.Format(「模型[{0}]。DogsName」,i.ToString()))沒有引發錯誤,但沒有顯示驗證消息,然後嘗試刪除模型Html.ValidationMessage( String.Format(「[{0}]。DogsName」,i.ToString()))和原始異常再次引發!我想這是向我們展示了MVC在做什麼,但並不能幫助我想出答案!還有什麼想法? – Andy 2010-07-23 08:27:27