简体   繁体   English

控件。添加从列表中删除以前的项目

[英]Controls.Add removing previous items from list

I am attempting to create a dynamic menu in my controller that i am then rendering to my view. 我试图在控制器中创建一个动态菜单,然后将其渲染到视图中。 What i am finding is that Controls.Add is removing items from my list and i end up only displaying the final item. 我发现,Controls.Add正在从列表中删除项目,而我最终仅显示最终项目。 How do i make sure that it displays all of the item in my list and how do I finish displaying the child elements since i am creating a dynamic menu? 创建动态菜单后,如何确保它显示列表中的所有项目以及如何完成显示子元素?

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; }

The problem is that you are adding the same instance of dropdownLi and mainMenuA in the loop over and over. 问题在于,您要一遍又一遍地在循环中添加dropdownLimainMenuA的相同实例。 I suppose that Control.Add will not add duplicates into the list, so the control is effectively added only once. 我想Control.Add不会将重复项添加到列表中,因此该控件仅被有效地添加了一次。 With the final run of the loop, you change the text to the text of the last item. 在循环的最后一次运行中,您将文本更改为最后一项的文本。

In order to fix this, you need to create new instances as follows: 为了解决此问题,您需要按如下方式创建新实例:

foreach (var item in navbarmenu)
{
    // Move the following lines from outside the loop inside
    HtmlGenericControl dropdownLi = new HtmlGenericControl("li");
    dropdownLi.Attributes.Add("class", "dropdown");
    HtmlGenericControl mainMenuA = new HtmlGenericControl("a");    
    mainMenuA.InnerText = item.MenuName;
    dropdownLi.Controls.Add(mainMenuA);
    navUl.Controls.Add(dropdownLi);                    
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM