MVC 3 中突出显示菜单和子菜单?

发布于 2024-11-19 11:59:58 字数 415 浏览 4 评论 0原文

我正在尝试创建一个突出显示当前页面的菜单。我在这里找到了一些答案,但问题是我看不到有人处理子菜单。

这里有一个看起来非常简单的答案: 活动菜单项 - asp. net mvc3 母版页

但据我所知,如果您单击子菜单,该母版页将仅突出显示子菜单项。我希望突出显示子菜单项及其顶部菜单中的父菜单项。

例如,如果有人单击“服务”,然后单击“咨询”,我希望这两个按钮都突出显示 - 顶部菜单中的“服务”和子菜单中的“咨询”。我该怎么做?

顺便说一句,我希望能够使用 CSS 将子菜单呈现为下拉菜单,也可以呈现为侧边栏。如何获取子菜单 ul 并将其呈现为侧边栏?

I am trying to create a menu that highlights the current page. I have found a few answers here, but the problem is I can't see that anyone handles submenus.

There is an answer here that looks enticingly simple: active menu item - asp.net mvc3 master page

But as far as I can tell, that one will highlight only the sub menu item if you click on a submenu. I want the submenu item to be highlighted, as well as its parent in the top menu.

E.g. if someone clicks Services, and then Consulting, I would want both of these to be highlighted - Services in the top menu, and Consulting in the submenu. How can I do this?

BTW, I would like to be able to render the submenu both as a dropdown using CSS, and also as a sidebar. How can I take the submenu ul and render it as a sidebar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲鸠 2024-11-26 11:59:58

这是一个简单的东西,你可以根据你的需要修改它,但基础知识就在这里。
http://developerstyle.posterous.com/highlighting-current -page-in-mvc-3-slick-tric

上面的链接可能会在Posterous关闭后立即关闭,这是一个更新链接
http://bhavinsurela.com/highlighting-current-page- in-mvc-3-slick-tric/

Here is a simple thing, you can modify it according to your needs but basics are here.
http://developerstyle.posterous.com/highlighting-current-page-in-mvc-3-slick-tric

This above link might be down soon as posterous is closing, here is an update link
http://bhavinsurela.com/highlighting-current-page-in-mvc-3-slick-tric/

只想待在家 2024-11-26 11:59:58

我有一个解决方案,我在 SO 中也找到了部分解决方案并进行了修改,但仍有待改进,以便处理任意数量的子菜单......现在它适用于子菜单。

namespace PhotoBuss.Web.Back.Controllers
{
public class NavigationController : BaseAdministrationController
{
    //
    // GET: /Common/

    [ChildActionOnly]
    public ActionResult HeaderMenu()
    {
        // http://stackoverflow.com/questions/4653226/asp-net-mvc-menu-selected-item

        var items = new List<MenuItemViewModel>
       {
           new MenuItemViewModel{ Text = "home", Action="Index", Controller="Administration", Selected=false},
           new MenuItemViewModel{Text = "manage", Action="Index", Controller="Manage", Selected=false,
           SubMenu = 
           new List<MenuItemViewModel>
           { 
               new MenuItemViewModel{ Text= "photos", Action="Index", Controller="Photos", Selected = false },
               new MenuItemViewModel { Text = "collections", Action="Index", Controller="Collections", Selected=false},
               new MenuItemViewModel { Text = "keywords", Action="Index", Controller="Keywords", Selected=false},
               new MenuItemViewModel { Text = "users", Action="Index", Controller="Users", Selected=false},
               new MenuItemViewModel { Text = "user groups", Action="Index", Controller="Roles", Selected=false}
           }
           },
           new MenuItemViewModel{Text="cms", Action="Index", Controller="CMS", Selected=false}
       };
        string action = ControllerContext.ParentActionViewContext.RouteData.Values["action"].ToString();
        string controller = ControllerContext.ParentActionViewContext.RouteData.Values["controller"].ToString();

        foreach (var item in items)
        {
            if (item.Controller == controller && item.Action == action)
            {
                item.Selected = true;
            }
            foreach(var subItem in item.SubMenu)
                if (subItem.Controller == controller && subItem.Action == action)
                {
                    item.Selected =
                    subItem.Selected = true;
                }
        }

        return PartialView(items);
    }
}

ViewModel

public class MenuItemViewModel
{
    public MenuItemViewModel()
    {
        SubMenu = new List<MenuItemViewModel>();
    }

    public string Text { get; set; }
    public string Controller { get; set; }
    public string Action { get; set; }
    public bool Selected { get; set; }

    public List<MenuItemViewModel> SubMenu { get; set; }
}
}

视图

    @model List<PhotoBuss.Web.Back.Models.Navigation.MenuItemViewModel>
    <link href="@Url.Content("~/Areas/Admin/Content/CSS/menu.css")" rel="stylesheet" type="text/css" />
    <div class="headerMenu">
        <ul>
            @foreach (var menuItem in Model)
            {
                <li>@Html.ActionLink(menuItem.Text, menuItem.Action, menuItem.Controller, null,
        new { @class = menuItem.Selected ? "selected" : "" })
                    @if (menuItem.SubMenu.Count >0)
                    {
                        <ul class="@(menuItem.Selected ? "selected" : "")">

                            @foreach (var subMenu in menuItem.SubMenu)
                            {
                                <li>@Html.ActionLink(subMenu.Text, subMenu.Action, subMenu.Controller, null,
        new { @class = subMenu.Selected ? "selected" : "" })</li>
                            }
                        </ul>
                    }
                </li>
            }
        </ul>
    </div>

CSS 我现在正在使用它:

    .headerMenu *
    {
        padding: 0;
        margin: 0;
    }
    .headerMenu
    {
        position: relative;
        background-color: #78C8FA;
        width: 100%;
        text-align: center;
        color: #FFFFFF;
        clear: both;
        float: left;
        margin-top: 10px;
    }
    .headerMenu ul
    {
        display: block;
        list-style: none;
        line-height: 3em;
        height: 3em;
    }

    .headerMenu ul li
    {
        display: inline-block;
        margin-left: 15px;
        margin-right: 15px;
    }

    .headerMenu ul li a
    {
        display: block;
        text-decoration: none;
        color: white;
        font-size: 1.5em;
        padding-left:2em;
        padding-right:2em;
    }

    .headerMenu ul li a:visited
    {
        color: white;
    }

    .headerMenu ul li a:hover, .menu ul li
    {
        color: #333333;
    }
    .selected
    {
        color: #333333 !important;
        display:block !important;
        background-color: #999999;
    }

    .headerMenu ul ul
    {
        display: none;
        position: absolute;
        width: 100%;
        right: 50%;
        left: 0;
        background-color: #999999;
    }

    .headerMenu li:hover > ul, .selected 
    {
        display: block;
    }

I have a solution which I in part also found here in SO and modified, but still as to be improved in order to handle any number of submenus... right now it works for a submenu.

namespace PhotoBuss.Web.Back.Controllers
{
public class NavigationController : BaseAdministrationController
{
    //
    // GET: /Common/

    [ChildActionOnly]
    public ActionResult HeaderMenu()
    {
        // http://stackoverflow.com/questions/4653226/asp-net-mvc-menu-selected-item

        var items = new List<MenuItemViewModel>
       {
           new MenuItemViewModel{ Text = "home", Action="Index", Controller="Administration", Selected=false},
           new MenuItemViewModel{Text = "manage", Action="Index", Controller="Manage", Selected=false,
           SubMenu = 
           new List<MenuItemViewModel>
           { 
               new MenuItemViewModel{ Text= "photos", Action="Index", Controller="Photos", Selected = false },
               new MenuItemViewModel { Text = "collections", Action="Index", Controller="Collections", Selected=false},
               new MenuItemViewModel { Text = "keywords", Action="Index", Controller="Keywords", Selected=false},
               new MenuItemViewModel { Text = "users", Action="Index", Controller="Users", Selected=false},
               new MenuItemViewModel { Text = "user groups", Action="Index", Controller="Roles", Selected=false}
           }
           },
           new MenuItemViewModel{Text="cms", Action="Index", Controller="CMS", Selected=false}
       };
        string action = ControllerContext.ParentActionViewContext.RouteData.Values["action"].ToString();
        string controller = ControllerContext.ParentActionViewContext.RouteData.Values["controller"].ToString();

        foreach (var item in items)
        {
            if (item.Controller == controller && item.Action == action)
            {
                item.Selected = true;
            }
            foreach(var subItem in item.SubMenu)
                if (subItem.Controller == controller && subItem.Action == action)
                {
                    item.Selected =
                    subItem.Selected = true;
                }
        }

        return PartialView(items);
    }
}

The ViewModel

public class MenuItemViewModel
{
    public MenuItemViewModel()
    {
        SubMenu = new List<MenuItemViewModel>();
    }

    public string Text { get; set; }
    public string Controller { get; set; }
    public string Action { get; set; }
    public bool Selected { get; set; }

    public List<MenuItemViewModel> SubMenu { get; set; }
}
}

The View

    @model List<PhotoBuss.Web.Back.Models.Navigation.MenuItemViewModel>
    <link href="@Url.Content("~/Areas/Admin/Content/CSS/menu.css")" rel="stylesheet" type="text/css" />
    <div class="headerMenu">
        <ul>
            @foreach (var menuItem in Model)
            {
                <li>@Html.ActionLink(menuItem.Text, menuItem.Action, menuItem.Controller, null,
        new { @class = menuItem.Selected ? "selected" : "" })
                    @if (menuItem.SubMenu.Count >0)
                    {
                        <ul class="@(menuItem.Selected ? "selected" : "")">

                            @foreach (var subMenu in menuItem.SubMenu)
                            {
                                <li>@Html.ActionLink(subMenu.Text, subMenu.Action, subMenu.Controller, null,
        new { @class = subMenu.Selected ? "selected" : "" })</li>
                            }
                        </ul>
                    }
                </li>
            }
        </ul>
    </div>

The CSS I'm using with this at the moment:

    .headerMenu *
    {
        padding: 0;
        margin: 0;
    }
    .headerMenu
    {
        position: relative;
        background-color: #78C8FA;
        width: 100%;
        text-align: center;
        color: #FFFFFF;
        clear: both;
        float: left;
        margin-top: 10px;
    }
    .headerMenu ul
    {
        display: block;
        list-style: none;
        line-height: 3em;
        height: 3em;
    }

    .headerMenu ul li
    {
        display: inline-block;
        margin-left: 15px;
        margin-right: 15px;
    }

    .headerMenu ul li a
    {
        display: block;
        text-decoration: none;
        color: white;
        font-size: 1.5em;
        padding-left:2em;
        padding-right:2em;
    }

    .headerMenu ul li a:visited
    {
        color: white;
    }

    .headerMenu ul li a:hover, .menu ul li
    {
        color: #333333;
    }
    .selected
    {
        color: #333333 !important;
        display:block !important;
        background-color: #999999;
    }

    .headerMenu ul ul
    {
        display: none;
        position: absolute;
        width: 100%;
        right: 50%;
        left: 0;
        background-color: #999999;
    }

    .headerMenu li:hover > ul, .selected 
    {
        display: block;
    }
誰認得朕 2024-11-26 11:59:58

只需使用 ViewContext.RouteData.Values 字典(特别是 Action 和 Controller 键)即可非常简单地确定要突出显示的菜单元素。

这是一个快速帮助方法:

public static string IsSelected(this RouteValueDictionary dictionary, string controller, string action)
{
    string cssClass = "selected";
    string routeValueController = dictionary["Controller"] as string;
    string routeValueAction = dictionary["Action"] as string;

    return string.IsNullOrEmpty(action) ? 
        routeValueController == controller ? cssClass : string.Empty : 
        routeValueController == controller && routeValueAction == action ? cssClass : string.Empty;
}

并且可以从这样的视图中使用:

<ul id="menu">
    <li class="@this.ViewContext.RouteData.Values.IsSelected("Default", "Index")">
        <a href="@Url.Action("Index", "Default")">Accueil</a>
    </li>
</ul>

由于我不熟悉您的应用程序结构,因此很难进入更具体的解决方案,但这应该会给您一个开始的想法。

It's fairly simple to determine which menu element to highlight by simply using the ViewContext.RouteData.Values dictionary, specifically the Action and Controller keys.

Here's a quick helper method :

public static string IsSelected(this RouteValueDictionary dictionary, string controller, string action)
{
    string cssClass = "selected";
    string routeValueController = dictionary["Controller"] as string;
    string routeValueAction = dictionary["Action"] as string;

    return string.IsNullOrEmpty(action) ? 
        routeValueController == controller ? cssClass : string.Empty : 
        routeValueController == controller && routeValueAction == action ? cssClass : string.Empty;
}

And can be used from the view as such :

<ul id="menu">
    <li class="@this.ViewContext.RouteData.Values.IsSelected("Default", "Index")">
        <a href="@Url.Action("Index", "Default")">Accueil</a>
    </li>
</ul>

It's hard to get into a more specific solution as I'm not familiar with your application structure, but this should give you an idea to get started.

醉酒的小男人 2024-11-26 11:59:58

这是一个处理子菜单并突出显示它的示例。

http://users.tpg.com.au/j_birch/plugins/superfish/ #sample4

它使用 superfish-navbar.css,您可以在其中看到它是如何完成的。
这是一个非常好的菜单插件。

Here is a example where they handle the submenus and highlight it.

http://users.tpg.com.au/j_birch/plugins/superfish/#sample4

It uses superfish-navbar.css where you can see how it is done.
It is a very good plugin for menus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文