I have a situation where I need to iterate through a small collection of query criteria and create a set of html fields for each. I would like to get unobtrusive js validation working for them, but of course, the problem is that you can’t use @Html.XxxFor(m => m.FieldName) because the field names need to be numbered – i.e.,
<input name="List[1].Category" />
<input name="List[1].Value" />
<input name="List[2].Category" />
<input name="List[2].Value" />
to get proper model binding and javascript behavior and whatnot. Consequently, I have to use:
@Html.Xxx("List[1].Category"...) such that I don’t get the unobtrusive validations emitted.
This all makes sense, but I’m wondering if anyone has found an elegant way to wire up unobtrusive validation in this scenario.
Thanks!
Here’s a code snippet which demonstrates how to use the
XxxFormethods and get name attributes which are indexed:This would avoid needing to wire-up unobtrusive validation manually.