1

How to dependency injection using Ninject in View in MVC 3? I am now using Ninject 2.2 and Ninject.MVC3 2.2.2.0

1
  • It would help if you were to explain what you want to do. Chances are, there is a better way to do it than what you think you have to do. Injecting code into the view is so far beyond a bad practice that you have to be going down a wrong path to even want to do it. Commented Jul 16, 2011 at 21:44

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what else if i want to manually write code straight to the View using some service?
Why do you want to do this? It's generally considered as bad practice. So why to you want to do a bad practice if anyone would tell you not to do so?
Because i have some partial view, that always come through the website, but i think it's not a good idea to get the data from database from all the controller to render it, i want to separate it, is there any other way instead of writing straight to the view?
0

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:

  1. DependencyResolver.Current.GetService<IViewService>(), but I don't recommend to do this way as this is well known "service locator" antipattern.
  2. Here I found a better approach

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.