如何从局部视图中获取父视图

Gar*_*ghi 16 asp.net-mvc-3

我有部分视图作为_Layout.cshtml的一部分,因此它在多个页面上呈现.将局部视图视为在网站上的每个页面上显示的菜单.

当点击局部视图菜单中的其中一个链接时,我只能访问/看到被称为局部视图的Action Method,就像它的名字一样.

但我真正需要的是在单击项目时部分视图处于打开状态的视图.

我怎么能得到这个?

Iri*_*dio 24

您可以使用ParentActionContext

例如

 var controller = ControllerContext.ParentActionViewContext.RouteData.Values["Controller"] as string;
 var action = ControllerContext.ParentActionViewContext.RouteData.Values["Action"] as string;
Run Code Online (Sandbox Code Playgroud)

更新

从视图来看,此调用应该满足您的需求

@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()
@HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString()
Run Code Online (Sandbox Code Playgroud)