如何从类别中创建多级菜单 [英] How to make multilevel menu from categories

查看:61
本文介绍了如何从类别中创建多级菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我有一个可以有多个子类别的类别模型。我想在菜单中显示这些类别,但只能降低一级。如何制作多级菜单解决方案?

我在局部视图中尝试使用辅助方法,但是它加载了一倍。



类别模型:

Hi I have an categories model that can have multiple subcategories. I want to show those categories in my menu, but can only get one level down. How do I make an multilevel menu soloution?
I have tried with an helper method inside the partial view, but it loads it double.

The Category model :

public class Category
    {
        public int CategoryId { get; set; }
        [Required(ErrorMessage = "Fill in a Name")]
        public string Name { get; set; }
        public int? ParentCategoryId { get; set; }
        public Category ParentCategory { get; set; }
        public virtual ICollection Listings { get; set; }
    }



填充局部视图的CategoriesController _Menu


CategoriesController that fill the partial view _Menu

[ChildActionOnly]
       public ActionResult GetMenu()
       {
           IList<Category> catAll = new List<Category>();
           catAll = categoriesRepository.All.ToList();
           return PartialView("_Menu", catAll);
       }



Partial view _Menu


The Partial view _Menu

@model List
    @foreach (var mp in Model.Where(p => p.ParentCategoryId == null))
    {
       
            @mp.Name
            
                @foreach (var menuLevel2 in Model.Where(p => p.ParentCategoryId.Equals(mp.CategoryId)))
                {
                    @menuLevel2.Name                 
                            @ShowTree(Model, mp.CategoryId);                    
                    }
                }
            
        
    }
    @helper ShowTree(List foos, int ParentId)
    {
foreach (var menuLevel2 in foos)
{
        if (menuLevel2.ParentCategoryId == ParentId)
        {
                    @menuLevel2.Name
        }
}
}

推荐答案

嗯,这里需要进行大量修改。



在局部视图中,您需要调用相同的局部视图(递归),但您的内部局部视图将具有子类别。

首先加载类别的局部视图,并在该局部视图中为子类别调用相同的视图。



请参考此主题 - http://stackoverflow.com/questions/16505300/loop-through-multi-level-dynamic-menus-in-asp -net-mvc [ ^ ]



-KR
Umm, lots of modification required here.

Within the partial view you need to call the same partial view (recursive), but your inner partial view will have the sub categories.
First you load the partial view for the category and within that partial view you called the same view for the subcategory.

Please refer this thread - http://stackoverflow.com/questions/16505300/loop-through-multi-level-dynamic-menus-in-asp-net-mvc[^]

-KR


这篇关于如何从类别中创建多级菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆