Getting current controller & action from within partial view

后端 未结 2 926
清酒与你
清酒与你 2020-12-23 22:29

I\'m using the following to get the current controller and action in asp.net MVC3:

var currentAction = routeData.GetRequiredString(\"action\");
var currentCo         


        
相关标签:
2条回答
  • 2020-12-23 22:36

    After your updated question and showing your code it is much more clear: you are not including a partial view. You are calling a child action. There's a huge difference between Html.Partial and Html.Action. So if you want to get the parent context inside this child action you could do this:

    public ActionResult Menu()
    {
        var rd = ControllerContext.ParentActionViewContext.RouteData;
        var currentAction = rd.GetRequiredString("action");
        var currentController = rd.GetRequiredString("controller");
        ...
        return View();
    }
    
    0 讨论(0)
  • 2020-12-23 22:54

    I stumbled on this page looking for a way to access the parent controllers name after a call using Partial

    @Html.Partial("Paging")
    

    This can be done in the partial view as

    @{
        var controller = ViewContext.RouteData.GetRequiredString("controller");
        var action = ViewContext.RouteData.GetRequiredString("action");
    }
    
    0 讨论(0)
提交回复
热议问题