I’m using MVC and i have a controller action which handles several different view models, each view model has validation and i’d like the controller to check the validation.
This is my controller action:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult WizardCaseResult(FormCollection fc)
{
ViewA vm = new ViewA();
TryUpdateModel<ViewA>(vm);
}
How do i change this code so that the type of view model can be set dynamically something like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult WizardCaseResult(FormCollection fc, string ViewType)
{
ViewType vm = new ViewType();
TryUpdateModel<ViewType>(vm);
}
I will probably have many different view models so a different action for each type is really out of the question.
You will need to write a custom model binder for this to work:
and then:
Now all you have to do is to ensure that the form sends the
ViewTypeparameter which will point to the view model that you want to instantiate.Oh, and you can forget about strong typing such as the following when dealing types that are only known at runtime:
You might also find the following answer helpful.