Reputation: 38180
When I try to get HomeController from View, it's always null, why ? Is it because I use CodeBehind (see my full source code Why my button click handler in codebehind is not called? )
public partial class Index : System.Web.Mvc.ViewPage<dynamic>
{
HomeController HomeController;
protected void Page_Load(object sender, EventArgs e)
{
HomeController = (HomeController)HttpContext.Current.Request.RequestContext.RouteData.Values["HomeController"];
Upvotes: 0
Views: 145
Reputation: 1038710
You could get the current controller from the ViewContext
. It has a Controller
property:
var currentController = ViewContext.Controller;
and if you know for sure that it's gonna be HomeController you could cast it:
var homeController = (HomeController)ViewContext.Controller;
This being said, I am not sure what exactly are you trying to achieve and why on Earth does your view has code behind, but this stinks from very far.
Upvotes: 2