StringBuilder
StringBuilder

Reputation: 1629

MVC 3 Action Dependency Injection

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.

Upvotes: 0

Views: 202

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

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.

Upvotes: 1

Related Questions