Ian Vink
Ian Vink

Reputation: 68830

Getting the Controller and Action used to call

In a Partial View _MyView.cshtml I need to get the Controller name and the Action used to create the parent calling view.

So if /Equity/Sell was the Controller/Action used to call the view which rendered the partial view _MyView.cshtml , then I need the value /Equity/Sell.

I don't see an object that can do that though.

Upvotes: 1

Views: 278

Answers (1)

Matthew
Matthew

Reputation: 9949

In the view use:

ViewContext.RouteData.GetRequiredString("controller");
ViewContext.RouteData.GetRequiredString("action");

For posterity in a child action it is like this:

ControllerContext.ParentActionViewContext.RouteData.Values["action"];
ControllerContext.ParentActionViewContext.RouteData.Values["controller"];

Upvotes: 2

Related Questions