1

I am using this code for generating menu, this menu populates items from database (Category Table) using this technique

Partial View :

@using SarbarzDarb.Helper
@model IEnumerable<SarbarzDarb.Models.Entities.Category>

@ShowTree(Model)

@helper ShowTree(IEnumerable<SarbarzDarb.Models.Entities.Category> categories)
{
        foreach (var item in categories)
        {
            <li class="@(item.ParentId == null && item.Children.Any() ? "dropdown-submenu" : "")">

                @Html.ActionLink(item.Name, actionName: "Category", controllerName: "Product", routeValues: new { Id = item.Id, productName = item.Name.ToSeoUrl() }, htmlAttributes: null)

                @if (item.Children.Any())
                {
                    ShowTree(item.Children);
                }

            </li>

        }
}

also I'm passing model from controller to above partial view this way :

public IList<Category> GetAll()
{

        return _category.Where(category => category.ParentId == null)
                        .Include(category => category.Children).ToList();
}
public ActionResult Categories()
{
            var query = GetAll();
            return PartialView("_Categories",query);
}

my Category Model:

public class Category
{
        public int Id { get; set; }
        public string Name { get; set; }

        public int? ParentId { get; set; }
        public virtual Category Parent { get; set; }

        public virtual ICollection<Category> Children { get; set; }


        public virtual ICollection<Product> Products { get; set; }

 }

Updated :
using recursive helpers are good idea but it doesn't generate sub-menu for me. what's my problem?
Any Idea?

Thanks in your advise

2
  • This question seems to have the answer: stackoverflow.com/questions/6422895/… Commented Oct 7, 2013 at 5:30
  • @hwiechers yes I know I've updated my answer, but still have problem. Commented Oct 7, 2013 at 6:14

1 Answer 1

10

finally I solved my problem by adding <ul class="dropdown-menu"> :

@using SarbarzDarb.Helper
@model IEnumerable<SarbarzDarb.Models.Entities.Category>
@ShowTree(Model)

@helper ShowTree(IEnumerable<SarbarzDarb.Models.Entities.Category> categories)
{
    foreach (var item in categories)
    {
    <li class="@(item.Children.Any() ? "dropdown-submenu" : "")">

        @Html.ActionLink(item.Name, actionName: "Category", controllerName: "Product", 
        routeValues: new { Id = item.Id, productName = item.Name.ToSeoUrl() }, htmlAttributes: null)
        @if (item.Children.Any())
        {
            <ul class="dropdown-menu">
                @ShowTree(item.Children)
            </ul>
                }
    </li>

        }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.