I've got a view model that contains 5 instances of a class as sub-properties. These sub-properties are rendered using a partial view, as follows:
<%Html.RenderPartial("_EntryItemForm", Model.EntryItem1, new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "EntryItem1" } }); %>
<%Html.RenderPartial("_EntryItemForm", Model.EntryItem2, new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "EntryItem2" } }); %>
<%Html.RenderPartial("_EntryItemForm", Model.EntryItem3, new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "EntryItem3" } }); %>
<%Html.RenderPartial("_EntryItemForm", Model.EntryItem4, new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "EntryItem4" } }); %>
<%Html.RenderPartial("_EntryItemForm", Model.EntryItem5, new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "EntryItem5" } }); %>
Within the partial view, I have the following (showing one field only):
<%: Html.LabelFor<EntryItemForm, string>(x => x.ItemName)%>
<%: Html.TextBoxFor<EntryItemForm, string>(x => x.ItemName)%>
<%: Html.ValidationMessageFor(x => x.ItemName)%>
The label and textboxes both render with the correct ids, names and so on, and the default model binder handles everything perfectly.
Unfortunately, even when the ModelState contains an error for the ItemName field, the ValidationMessage never appears. If I add a ValidationSummary to the parent view, the error is displayed. Normally I'd just use a ValidationSummary and move on, but the design I'm working to requires inline validation messages.
Does anyone have any idea why this might be?