Here is the buddy class used to add data annotation validators to a Entity Framework model:
[MetadataType(typeof(Result_Validation))]
public partial class Result
{
}
[Bind(Include = "Title,Description")]
public class Result_Validation
{
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }
[Required(ErrorMessage = "Description is required")]
public string Description { get; set; }
}
Here is my view:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IList<Result>>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<% for (int i = 0; i < 4; i++) { %>
<div class="editor-label">
<%: Html.LabelFor(model => model[i].Title)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model[i].Title)%>
<%: Html.ValidationMessageFor(model => model[i].Title)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model[i].Description)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model[i].Description)%>
<%: Html.ValidationMessageFor(model => model[i].Description)%>
</div>
<% } %>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
</body>
</html>
This works fine for server side validaton, but as soon as I drop the Html.EnableClientValidation(); in there I get this error: "The given key was not present in the dictionary."
..and it points to this line in the view:
<%: Html.ValidationMessageFor(model => model[i].Title)%>