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.

Richard
  • 29,854
  • 11
  • 77
  • 120
StringBuilder
  • 1,619
  • 4
  • 32
  • 52

1 Answers1

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks man , I've justr ealized , that dependency injection into Action is useless , thank for pointing in rigth direction – StringBuilder Dec 02 '12 at 11:29