获取电流控制器&放大器;从局部视图中的作用 [英] Getting current controller & action from within partial view

查看:144
本文介绍了获取电流控制器&放大器;从局部视图中的作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的获得在asp.net MVC3电流控制器和行动:

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

var currentAction = routeData.GetRequiredString("action");
var currentController = routeData.GetRequiredString("controller");

这完美的作品。但是,如果我把这个距离是从我的布局称为局部视图中,布局作为返回电流控制器。这当然是正确的行为,但有什么办法再次访问父控制器的名字?

This works perfectly. However, if I call this from within a partial view that is called from my layout, "Layout" is returned as the current controller. This is of course correct behaviour, but is there any way to again access the parent controller's name?

感谢您提前。

修改为进一步澄清:

Edit for further clarification:

我呼吁我的菜单控制器和局部视图从内部_Layout.cshtml:

I am calling my menu controller and partial view from within _Layout.cshtml:

@Html.Action("Menu", "Layout")

然后从菜单中部分观点中,我调用code返回当前操作和控制。

Then from within that Menu partial view, I am calling code which returns the current action and controller.

推荐答案

更新的问题,显示你的code它是更清楚后:你是不是其中的局部视图。你调用一个孩子的行动。有 Html.Partial Html.Action 之间的巨大差异。所以,如果你想获得这个孩子动作里面的父上下文,你可以这样做:

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();
}

这篇关于获取电流控制器&放大器;从局部视图中的作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆