Hi all,
I've used stackoverflow many a time to solve my problems (code related!) but this is the first time I've needed to post a question cause I cannot work out whats wrong.
When I enable client side validation on a view that allows editing of a collection of objects which use DataAnnotations for validation the following exception is raised:
[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)%>
If I remove the <% Html.EnableClientValidation(); %>
call no exception is raised and server side validation works as expected.
Here is my test Model:
public class Dog {
[Required]
public string Name { get; set; }
}
and view:
<%@ 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>
<%} %>
Any ideas on how to get this working would be appreciated as its been doing my head in trying to get this to work!!!
Thanks