Click here to Skip to main content
15,667,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C#
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 that fill the partial view _Menu
C#
[ChildActionOnly]
       public ActionResult GetMenu()
       {
           IList<Category> catAll = new List<Category>();
           catAll = categoriesRepository.All.ToList();
           return PartialView("_Menu", catAll);
       }

The Partial view _Menu
C#
@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
        }
}
}
Posted
Updated 26-Nov-15 11:42am
v5

1 solution

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
 
Share this answer
 
Comments
tina_overgaard 27-Nov-15 6:06am    
Thanks for the tip, I will try that :-)
Krunal Rohit 27-Nov-15 6:48am    
Glad I could help. :)
I guess I've solved some your issues before. You're newbie in MVC, right ?

-KR
tina_overgaard 27-Nov-15 8:21am    
Yes really an newbie, just started in August. But I love coding :-). I have lidt issues with that example you gave me, how do I do this Children = CreateVM(m.CategoryId, source) there is an error on source because that is an list an the method is returning an IEnumerable??
Krunal Rohit 27-Nov-15 23:06pm    
Convert your IEnumerable to List by .ToList();

-KR
tina_overgaard 27-Nov-15 23:32pm    
I have try that, it still comes with an error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900