如何从Child动作中获取当前控制器和动作?

JBe*_*ton 55 asp.net-mvc asp.net-mvc-routing

我有一部分视图通过RenderAction调用子动作呈现.如何从此子操作中获取父控制器和操作.

我用的时候..

@ViewContext.RouteData.Values["action"]
Run Code Online (Sandbox Code Playgroud)

我找回了Child Action的名字,但我需要的是Parent/Calling动作.

谢谢

顺便说一下,我正在使用带Razor的MVC 3.

Rup*_*tes 73

如果你想从子动作本身(而不是视图)中访问它,你可以使用

ControllerContext.ParentActionViewContext.RouteData.Values["action"] 
Run Code Online (Sandbox Code Playgroud)


Car*_*z T 17

如果partial在另一个partial内,除非我们找到最顶层的父视图内容,否则这将不起作用.你可以用这个找到它:

var parentActionViewContext = ViewContext.ParentActionViewContext;
while (parentActionViewContext.ParentActionViewContext != null)
{
    parentActionViewContext = parentActionViewContext.ParentActionViewContext;
}
Run Code Online (Sandbox Code Playgroud)