In my Layout.cshtml I call RenderAction to show the menu for every page request:
Html.RenderAction("NiceMenu", "Widgets", new {area = ""});
The WidgetController needs to know the parent executing Controller and Action calling it so that it can render the menu with the right item highlighted.
How can the Widget Controller's NiceMenu action know this?
this is how we do it
var action = (ViewContext.RouteData.Values["action"] ?? "").ToString().ToLower();
var controller = (ViewContext.RouteData.Values["controller"] ?? "").ToString().ToLower();
var area = (ViewContext.RouteData.DataTokens["area"] ?? "").ToString().ToLower();
You can use ParentActionViewContext property of ViewContext in the child action's view:
var parentRouteValues = ViewContext.ParentActionViewContext.RouteData.Values;
@Html.RenderAction("NiceMenu", "Widgets",
new
{
area = parentRouteValues["area"],
controller = parentRouteValues["controller"],
action = parentRouteValues["action"]
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With