如何使用递归循环填充菜单?

时间:2016-03-17 11:40:11

标签: asp.net-mvc recursion

以下是信息。

不知道如何循环View中的SubNav项。 (参考:Loop Through Multi-Level Dynamic Menus in Asp.Net MVC

控制器

[ChildActionOnly]
    public PartialViewResult LoadNav()
    {
        var db = new NavDb();
        List<Nav> NavCol = db.Navs.ToList<Nav>();
        var navObj = CreateNavVM(0, NavCol);
        return PartialView("_PVTopNav", navObj);
    }
    public IEnumerable<NavViewModel> CreateNavVM(int pParentId, List<Nav> pNavCol)
    {
        return from m in pNavCol
               orderby m.DisplaySeq
               where m.ParentMenuId == pParentId
               select new NavViewModel()
               {
                   MenuId = m.MenuId,
                   Name = m.Name,
                   HtmlTitle = m.HtmlTitle,
                   Url = m.Url,
                   DisplaySeq = m.DisplaySeq,
                   SubNav = (IEnumerator<NavViewModel>)CreateNavVM(m.MenuId, pNavCol)
               };
    }

视图模型

public class NavViewModel
{
    public int MenuId { get; set; }
    public int ParentMenuId { get; set; }
    public string Name { get; set; }
    public string HtmlTitle { get; set; }
    public string Url { get; set; }
    public int DisplaySeq { get; set; }
    public IEnumerator<NavViewModel> SubNav { get; set; }
}

模型

public class Nav
{
    [Key]
    public int MenuId { get; set; }
    public int ParentMenuId { get; set; }
    public string Name { get; set; }
    public string HtmlTitle { get; set; }
    public string Url { get; set; }
    public int DisplaySeq { get; set; }
}

查看

    @model IEnumerable<yetpweb.ViewModels.NavViewModel>

    @foreach (var m in Model) {
        <div class="ui simple dropdown item">
            @m.Name
            <div class="menu">
                <a class="item" href="#">Link Item</a>
                <a class="item" href="#">Link Item</a>
                <div class="item">
                    <i class="dropdown icon"></i>
                    Sub Menu
                    <div class="menu">
                        <a class="item" href="#">Link Item</a>
                        <a class="item" href="#">Link Item</a>
                    </div>
                </div>
                <a class="item" href="#">Link Item</a>
            </div>
        </div>
    }

1 个答案:

答案 0 :(得分:1)

你可以开始另一个razor foreach声明。在您的代码中,这将导致:

@foreach (var m in Model) {
        <div class="ui simple dropdown item">
            @m.Name
            <div class="menu">
                <a class="item" href="#">Link Item</a>
                <a class="item" href="#">Link Item</a>
                <div class="item">
                    <i class="dropdown icon"></i>
                    @foreach(var sub in m.SubNav) {
                    <div class="menu">
                        <a class="item" href="#">Link Item</a>
                        <a class="item" href="#">Link Item</a>
                    </div>
                    }
                </div>
                <a class="item" href="#">Link Item</a>
            </div>
        </div>
    }