1
我試圖在我的控制器中創建一個動態菜單,然後渲染到我的視圖。我發現的是,Controls.Add從我的列表中刪除項目,我最終只顯示最終項目。我如何確保它顯示列表中的所有項目,以及如何完成顯示子元素,因爲我正在創建一個動態菜單?Controls.Add從列表中刪除以前的項目
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");
HtmlGenericControl dropdownLi = new HtmlGenericControl("li");
dropdownLi.Attributes.Add("class", "dropdown");
HtmlGenericControl mainMenuA = new HtmlGenericControl("a");
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 item in navbarmenu)
{
mainMenuA.InnerText = item.MenuName;
dropdownLi.Controls.Add(mainMenuA);
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; }
感謝的人。有效。 – Nakres