ASP.Net MVC 3: Dependency injection with Ninject in View
How to dependenc开发者_运维知识库y injection using Ninject in View in MVC 3? I am now using Ninject 2.2 and Ninject.MVC3 2.2.2.0
You shouldn't be using Ninject
to supply anything to the View
. Ninject
supplies dependencies to the Controllers
, it is the job of the controller to build and pass models required by the View
.
There are plenty of good tutorials around, see here for example.
Take a look on previous answers, do you really need this? View injection is in contradiction with MVC pattern and should be avoided in most cases.
But if you need some service to render some parts of a view you can do following:
DependencyResolver.Current.GetService<IViewService>()
, but I don't recommend to do this way as this is well known "service locator" antipattern.- Here I found a better approach
精彩评论