列表上的递归循环MVC



我的网站上有一个侧菜单,目前是一个列表项的负载:

<ul>
    <li>...</li>
    <li>...</li>
    <li>...</li>
</ul>

现在我需要改变这个有子项,所以它呈现…

<ul>
    <li>
        <ul>
            <li>...</li>
            <li>...</li>
        </ul>
    </li>
    <li>...</li>
    <li>...</li>
</ul>

我有一个项目列表,如下所示:

    /// <summary>
    /// Gets the admin menu items.
    /// </summary>
    /// <returns></returns>
    public IQueryable<IAdminMenuItem> GetAdminMenuItems()
    {
        return new List<IAdminMenuItem> {              
            new AdminMenuItem {MenuItemId = "100", DisplayText = "xxx", ParentId = "" ControllerName = "xxx", ActionName = "xxx"},
            new AdminMenuItem {MenuItemId = "101", DisplayText = "xxx", ParentId = "100" ControllerName = "xxx", ActionName = "xxx"},
            new AdminMenuItem {MenuItemId = "102", DisplayText = "xxx", ParentId = "100" ControllerName = "xxx", ActionName = "xxx"},
            new AdminMenuItem {MenuItemId = "200", DisplayText = "xxx", ParentId = "" ControllerName = "xxx", ActionName = "xxx"},
            new AdminMenuItem {MenuItemId = "201", DisplayText = "xxx", ParentId = "200" ControllerName = "xxx", ActionName = "xxx"},
            new AdminMenuItem {MenuItemId = "202", DisplayText = "xxx", ParentId = "200" ControllerName = "xxx", ActionName = "xxx"},
        }.AsQueryable();

如果列表项没有ParentID,则它是顶级元素。如果是,则为子项元素。

我现在已经有了这个代码来处理平面菜单结构:

<ul>
    @foreach (Avelo.Exchange.WebUI.Domain.Interfaces.IAdminMenuItem item in Model){
       <li><a href="@Url.Action(item.ActionName, item.ControllerName)"><span>@item.DisplayText</span></a></li>
    }
</ul>

我想我的第一个问题是我如何处理视图中的子菜单项,但我也想通过它能够处理进一步的子级别(即三个步骤而不是两个)来证明未来的解决方案。我听说过递归循环,但不知道如何实现它。这是个好办法吗?

Thanks in advance

S

我以前也遇到过类似的问题。我将设法使我的解适合你的模型。我猜你是这样写的:

public interface IAdminMenuItem
{
    string MenuItemId { get; set; }
    string DisplayText { get; set; }
    string ControllerName { get; set; }
    string ActionName { get; set; }
    List<IAdminMenuItem> Children { get; set; }
}
public class AdminMenuItem: IAdminMenuItem
{
    public string MenuItemId { get; set; }
    public string DisplayText { get; set; }
    public string ControllerName { get; set; }
    public string ActionName { get; set; }
    public List<IAdminMenuItem> Children { get; set; }
}

你可以看到,我已经改变了你的属性ParentId与儿童的集合,因为它更容易管理。

在您的视图中,您将填充您的菜单(和所有子菜单)并将其传递给视图:

List<IAdminMenuItem> myMenu = new List<IAdminMenuItem>();
myMenu.Add(new AdminMenuItem()
    {
    MenuItemId = "100",
    DisplayText = "Level 1",
    ControllerName = "Home",
    ActionName = "Index",
    Children = new List<IAdminMenuItem>() { 
        new AdminMenuItem() { MenuItemId = "1001", DisplayText = "Level 1a", ControllerName = "Home", ActionName = "Index" },
        new AdminMenuItem() { MenuItemId = "1002", DisplayText = "Level 1b", ControllerName = "Home", ActionName = "Index" }
    }
});
myMenu.Add(new AdminMenuItem()
    {
    MenuItemId = "200",
    DisplayText = "Level 2",
    ControllerName = "Home",
    ActionName = "Index",
    Children = new List<IAdminMenuItem>() { 
        new AdminMenuItem() { MenuItemId = "2001", DisplayText = "Level 2a", ControllerName = "Home", ActionName = "Index" },
        new AdminMenuItem() { MenuItemId = "2002", DisplayText = "Level 2b", ControllerName = "Home", ActionName = "Index" }
    }
});
return View(myMenu);

这是我的观点。这里的东西不多。

@using Mvc3SubMenus.WebUI
@model List<Mvc3SubMenus.IAdminMenuItem>
@{
    ViewBag.Title = "Home Page";
}
@Html.BuildMenu(Model)

我已经创建了一个助手,它将负责创建您的菜单和所有子菜单(递归):

using System.Collections.Generic;
using System.Web.Mvc;
using System.Text;
using System.Web.Mvc.Html;
namespace Mvc3SubMenus.WebUI
{
    public static class MyHelpers
    {
        public static System.Web.Mvc.MvcHtmlString BuildMenu(this HtmlHelper helper, List<Mvc3SubMenus.IAdminMenuItem> menu)
        {
            return new MvcHtmlString(BuildStringMenu(helper, menu));
        }
        private static string BuildStringMenu(HtmlHelper helper, List<Mvc3SubMenus.IAdminMenuItem> menu)
        {
            var sb = new StringBuilder();
            if ((menu != null) && (menu.Count > 0))
            {
                sb.Append("<ul>");
                foreach (var item in menu)
                {
                    sb.Append("<li>");
                    sb.Append(helper.ActionLink(item.DisplayText, item.ActionName, item.ControllerName));
                    sb.Append("</li>");
                    if ((item.Children != null) && (item.Children.Count > 0))
                    {
                        sb.Append("<li>");
                        sb.Append(BuildStringMenu(helper, item.Children));
                        sb.Append("</li>");
                    }
                }
                sb.Append("</ul>");
            }
            return (sb.ToString());
        }
    }
}

这个助手可以改进,当然有人可能会反对它可以用更好的方式完成,但我没有太多的时间来完善它。很抱歉。
您可以在这里找到一些代码。项目名为Mvc3SubMenus

最新更新