MVC菜单儿童

时间:2016-01-19 21:21:10

标签: asp.net-mvc asp.net-mvc-4

我有一个从数据库中填充的MVC菜单。我有代码可以到达第4个菜单的孩子。我想知道是否有更好的方法在我的cshtml中编写此代码,以便它自动检测菜单子项,我不必硬编码菜单到一定程度。谢谢你的帮助。

      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                @foreach (var menuLevel1 in navbarmenu)
                {
                    <li class="dropdown">

                        <a href="~/CATS/Default.aspx">
                            @menuLevel1.MenuName<span class="caret"></span>
                        </a>
                        @{var navbarsubmenu = myMenu.Where(x => x.ParentID == menuLevel1.MenuID && x.IsDeleted == false && x.IsEnabled == true).ToList().OrderBy(x => x.MenuName);}
                        @if (navbarsubmenu.IsAny())
                        {
                            <ul class="dropdown-menu">
                                @foreach (var menuLevel2 in navbarsubmenu)
                                {
                                    <li class="dropdown-submenu">
                                        @if (menuLevel2.MenuLink != null && menuLevel2.MenuLink != "")
                                        {
                                            <a href="@Url.Content(menuLevel2.MenuLink)">
                                                @menuLevel2.MenuName
                                            </a>
                                        }
                                        else
                                        {
                                            <a href="~/CATS/Default.aspx">
                                                @menuLevel2.MenuName
                                            </a>
                                        }
                                        @{var navbarsubmenu2 = myMenu.Where(x => x.ParentID == menuLevel2.MenuID && x.IsDeleted == false && x.IsEnabled == true).ToList().OrderBy(x => x.MenuName);}
                                        @if (navbarsubmenu2.IsAny())
                                        {

                                            <ul class="dropdown-menu">
                                                @foreach (var menuLevel3 in navbarsubmenu2)
                                                {
                                                    <li class="dropdown-submenu">
                                                        @if (menuLevel3.MenuLink != null && menuLevel3.MenuLink != "")
                                                        {
                                                            <a href="@Url.Content(menuLevel3.MenuLink)">
                                                                @menuLevel3.MenuName
                                                            </a>
                                                        }
                                                        else
                                                        {
                                                            <a href="~/CATS/Default.aspx">
                                                                @menuLevel3.MenuName
                                                            </a>
                                                        }
                                                        @{var navbarsubmenu3 = myMenu.Where(x => x.ParentID == menuLevel3.MenuID && x.IsDeleted == false && x.IsEnabled == true).ToList().OrderBy(x => x.MenuName);}
                                                        @if (navbarsubmenu3.IsAny())
                                                        {

                                                            <ul class="dropdown-menu">
                                                                @foreach (var menuLevel4 in navbarsubmenu3)
                                                                {
                                                                    <li class="dropdown-submenu">
                                                                        @if (menuLevel4.MenuLink != null && menuLevel4.MenuLink != "")
                                                                        {
                                                                            <a href="@Url.Content(menuLevel4.MenuLink)">
                                                                                @menuLevel4.MenuName
                                                                            </a>
                                                                        }
                                                                        else
                                                                        {
                                                                            <a href="~/CATS/Default.aspx">
                                                                                @menuLevel4.MenuName
                                                                            </a>
                                                                        }
                                                                    </li>
                                                                }
                                                            </ul>
                                                        }
                                                    </li>
                                                }
                                            </ul>
                                        }
                                    </li>

                                }
                            </ul>
                        }
                    </li>
                }
            </ul>

1 个答案:

答案 0 :(得分:2)

StephenMuecke推荐的递归方法

    public static class MenuChildExtensions
    {
        public static MvcHtmlString MenuChild(this HtmlHelper helper, Menu menu)
        {
            return MvcHtmlString.Create(MenuChildren(menu));
        }

        private static string MenuChildren(Menu menu)
        {
            StringBuilder html = new StringBuilder();
            TagBuilder div = new TagBuilder("div");
            div.InnerHtml = menu.Name;
            html.Append(div.ToString());
            if (menu.MenuItems != null)
            {
                foreach (Menu menuchildMenu in menu.MenuItems)
                {
                    html.Append(MenuChildren(menuchildMenu));
                }

            }

            TagBuilder item = new TagBuilder("li");
            item.InnerHtml = html.ToString();
            TagBuilder container = new TagBuilder("ul");
            container.InnerHtml = item.ToString();
            return container.ToString();
        }
    }
}