如何使菜单动态增长C#

时间:2016-02-11 21:29:25

标签: c# linq

我有一个菜单,我从List填充,我希望子菜单动态显示取决于从列表返回的内容。目前我能够显示两个级别,我可以继续添加foreach循环以添加更多级别,但我想动态这样,以便菜单不受逻辑中循环次数的限制。

public string CreateDynamicMenu()
        {
            HtmlGenericControl navbar = new HtmlGenericControl("nav");
            navbar.Attributes.Add("class", "navbar navbar-default");
            HtmlGenericControl containerDiv = new HtmlGenericControl("div");
            containerDiv.Attributes.Add("class", "container-fluid");
            HtmlGenericControl collapseDiv = new HtmlGenericControl("div");
            collapseDiv.Attributes.Add("class", "collapse navbar-collapse");
            HtmlGenericControl navUl = new HtmlGenericControl("ul");
            navUl.Attributes.Add("class", "nav navbar-nav");

            //  mainMenuA.Attributes.Add("href", "~/Default.aspx");

            var myMenu = menuListForUserG.GetMenus(Global.CurrentProfile.UserID).OrderBy(x => x.MenuOrder).ThenBy(y => y.MenuName);
            var navbarmenu = myMenu.Where(x => x.ParentID == null);

            foreach (var menuLevel1 in navbarmenu)
            {
                HtmlGenericControl dropdownLi = new HtmlGenericControl("li");
                dropdownLi.Attributes.Add("class", "dropdown");
                HtmlGenericControl mainMenuA = new HtmlGenericControl("a");
                mainMenuA.Attributes.Add("href", "~/Default.aspx");
                mainMenuA.InnerText = menuLevel1.MenuName;
                dropdownLi.Controls.Add(mainMenuA);



                var navbarsubmenu = myMenu.Where(x => x.ParentID == menuLevel1.MenuID);
                if (navbarsubmenu.IsAny())
                {
                    HtmlGenericControl dropdownUl = new HtmlGenericControl("ul");

                    dropdownUl.Attributes.Add("class", "dropdown-menu");
                    foreach (var menuLevel2 in navbarsubmenu)
                    {
                        HtmlGenericControl dropdownsubmenuLi = new HtmlGenericControl("li");
                        dropdownsubmenuLi.Attributes.Add("class", "dropdown-submenu");
                        HtmlGenericControl subMenuA = new HtmlGenericControl("a");
                        subMenuA.Attributes.Add("href", "~/Default.aspx");
                        subMenuA.InnerText = menuLevel2.MenuName;
                        dropdownsubmenuLi.Controls.Add(subMenuA);
                        dropdownUl.Controls.Add(dropdownsubmenuLi);
                        dropdownLi.Controls.Add(dropdownUl);
                        navUl.Controls.Add(dropdownLi);
                    }

                }
            }

            collapseDiv.Controls.Add(navUl);
            containerDiv.Controls.Add(collapseDiv);
            navbar.Controls.Add(containerDiv);


            StringBuilder htmlStringBuilder = new StringBuilder();
            HtmlTextWriter htmlStringWriter = new HtmlTextWriter(new StringWriter(htmlStringBuilder));
            navbar.RenderControl(htmlStringWriter);
            String output = htmlStringBuilder.ToString();
            return output;
        }

1 个答案:

答案 0 :(得分:1)

我终于学会了如何使用递归。我在视图中使用了一个辅助类,它解决了这个问题。它是一个无限级别的动态菜单。

   @model IEnumerable<Menus>

@helper GetSubMenus(IEnumerable<Menus> siteMenu, int? parentID)
{  
    foreach (var i in Model.Where(a => a.ParentID.Equals(parentID)))
    {       
        <li class="@(i.ParentID.HasValue ? "dropdown-submenu" : "dropdown")">
            <a href="@(!string.IsNullOrEmpty(i.MenuLink) ? Url.Content(i.MenuLink) : "~/ADDS/Default.aspx")" style="@(i.ParentID.HasValue ? "" : "font-size:16px;")">@i.MenuName</a>
            @if (Model.Any(a => a.ParentID.Equals(i.MenuID)))
            {                             
                <ul class="dropdown-menu">
                    @GetSubMenus(siteMenu, i.MenuID)
                    @* Recursive  Call for Populate Sub items here*@
                </ul>
            }
        </li>
    }
}

@if (@Model.IsAny())
{
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class=" nav navbar-nav">
                    @GetSubMenus(@Model, @Model.First().ParentID)
                </ul>
            </div>
        </div>
    </nav>
}