How do I tell unobstructive validation in MVC 3 to use a different model?

Go To


I am upgrading an application from MVC 1 to MVC 3. I previously was using xVal and I am attempting to use the new Unobstructive validation. In my existing code, I often have the following.

<%=Html.ClientSideValidation<Applicant>() %>

Whereas, the model I pass to the page is a ViewModel that contains additional information. With the new validation, it wants to look at my model I pass to the page in order to find validators. I can change my referencess when I use the HTML Helpers to include a prefix. If I do this, it picks up the validation. However, I would like to tell the page to use my entity model vs my view model so that I do not have to change all of my existing controllers to look for this prefix.

Is this possible?

2012-04-03 20:07
by Jonathan


You can use a different model to both render your fields and having them validated. This is useful when the model passed to the view is different from the model used by the action method you would like to post. Both the action method to post to and the "client validation" should refer to the same model in order to ensure that both Client Side and ServerSide validation work. I am the coordinator of the Mvc Controls Toolkit project. There, there are tools to create dynamically html objects you can use to render fields of an arbirary model that is not necessarely the page ViewModel. In particular you might use the crossHelper extension method. However I am not 100% sure this is exactly what you asked for.

2012-04-06 16:27
by Francesco Abbruzzese


Considering you don't want to perform massive edits, you could write a custom HTML Helper to include that prefix you talk about for you.

If you provide me further information I could help you write the helper. But you will have to change the actual helper calls.

2012-04-10 00:21
by Esteban