Make the controller available to the views that it creates
I would like to be able to add specific methods to a view that is created, or at the very least have the public methods off of the controller itself available to the view.
Jens Hofmann commented
ViewContext.Controller is what you are searching for. But you should really think about carefully whether to use it or not.
This certainly violates MVC pattern. If you have the urge to get to the controller from the view, you need to double check your design.
Jeffrey Palermo commented
If there is a property of the controller whose value you need in the view, consider sending that value/object to the view explicitly. Allowing the view to "poke around" back at the controller could add to unnecessary coupling. Sending another object to the view instead will preserve the one-way and explicit relationship between controller and view.
Nikos Baxevanis commented
-1! It seems someone missed WinForms :)
Stephen Burns commented
No thanks. Views should know nothing about controllers. Review the actual MVC pattern please.
James Kovacs commented
-1! I cannot express how bad an idea this is. One of the main points of MVC is decoupling the view from the controller/model. If you need methods in your view, use HTML helpers.
This is bad for design!
Hmm, I wouldn't do it. What methods would you like to call from the view? Every example I can think of sends up flags saying 'please don't do this'.
Koen Willemse commented
Not a good idea.The only code the view should incorperate is some basic looping to generate the correct html, not much more.
Brendan Enrick commented
One of the reasons for MVC's existence is to prevent this type of logic from being in the view.