I'm experimenting with the unobtrusive client validation and have been struggling to get it to work in my scenario.
My main problem is that my view has a strongly typed ViewModel. This ViewModel contains several collections that are used to populate dropdowns and has an additional property (let's call it Person) which is the actual object I wish the form to edit.
My controller action handling the post expects to get a Person object not the entire ViewModel posted. So I haven't been able to use:
@Html.TextBoxFor(m => m.Person.Name)
because I need the input control to have a name = "Name" and not "Person.Name" I can correct this by using
@Html.TextBox("Name", m.Person.Name)
But then the resulting input control lacks the data-* attributes client validation needs. Is there a way to get client validation to work, keep my viewmodel, and make my controller action all work together?
On a side note it seems like the data-* attributes on form fields are only added if you use
@using (Html.BeginForm())
Writing out my own <form>
tag by hand seems to break it.
Is using the form helper required?