如何使菜单动态增长 C#
本文关键字:动态 何使 菜单 | 更新日期: 2023-09-27 18:34:12
我有一个从列表中填充的菜单,我希望子菜单根据从列表中返回的内容动态显示。目前,我能够显示两个级别,我可以继续添加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;
}
我终于学会了如何使用递归。我在视图中使用了一个帮助程序类,它解决了问题。这是一个无限级别的动态菜单。
@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>
}