0

Maybe it will dup , but I didn't found anything that will help me to fix the problem.

I have Controller that can Have different ViewModels :

I would like to do something like:

[HttpPost]
public ActionResult ExportToExcell(ILeadsViewModel model)
{


}

My ViewModels are:

public class ViewModelTest1 : ILeadsViewModel

public class ViewModelTest2 : ILeadsViewModel

public class ViewModelTest3 : ILeadsViewModel

Is there any chance to configure Unity return right instance for my ViewModel ? If possible how ? If possible , but you dont suggesting to this , please explain why.

Thanks.

2
  • I'm interested in how different they are and what that gives you. Dec 2, 2012 at 10:00
  • Each viewModel contains nested Model that can differ depends on user request , and then data rendering different for each type of view model Dec 2, 2012 at 10:05

1 Answer 1

1

You could write a custom model binder for the ILeadsViewModel type that will return the proper view model based on some request properties. Here's an example of a custom model binder. In this example I have used a Type property present in the request to Activator.CreateInstance the concrete view model but you could use Unity or whatever. But bear in mind that you should have some information present in the request that would allow the model binder to know which concrete instance of the view model to return.

1
  • Thanks man , I've justr ealized , that dependency injection into Action is useless , thank for pointing in rigth direction Dec 2, 2012 at 11:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.