开发者

asp.net mvc c# highlight selected menu item by detecting route in partial view menu

Just thought I'd give a little back, since I've found many solutions here on stackoverflow but have not yet posted an answe开发者_如何学JAVAr (being an asp.net mvc novice):

Having implemented a menu system as a partial view rendered by my site's master page, I wanted the menu's controller to highlight the "selected" menu item, to give the user a sense of where they were on the site. The problem was that when the menu is being rendered the current routedata is that of the menu itself, not of the main content page. Therefore, how would the menu controller know which menu item to highlight? I thought my solution was quite neat, but perhaps you will have other suggestions:

public class NavController : Controller
{
    private string parentAction { get; set; }
    private string parentController { get; set; }

    protected override void OnActionExecuting(ActionExecutingContext ctx)
    {
        base.OnActionExecuting(ctx);
        parentAction = ctx.ParentActionViewContext.Controller.ControllerContext.RouteData.Values["action"].ToString();
        parentController = ctx.ParentActionViewContext.Controller.ControllerContext.RouteData.Values["controller"].ToString();
    }

This menu-generating class can now use "parentAction" and "parentController" to highlight the menu item that corresponds to the "parent" page, i.e. the page that the user is looking at in the main content part of the master.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜