title: ASP.NET MVC : Generating multi level menu using recursive helpers
body: <p>I am using this code for generating menu, this menu populates items from database (Category Table) using <a href="https://stackoverflow.com/questions/6422895/mvc3-razor-recursive-function">this technique</a>  </p>
  
  <p><strong>Partial View :</strong></p>
  
  <pre><code>@using SarbarzDarb.Helper
  @model IEnumerable&lt;SarbarzDarb.Models.Entities.Category&gt;
  
  @ShowTree(Model)
  
  @helper ShowTree(IEnumerable&lt;SarbarzDarb.Models.Entities.Category&gt; categories)
  {
          foreach (var item in categories)
          {
              &lt;li class="@(item.ParentId == null &amp;&amp; item.Children.Any() ? "dropdown-submenu" : "")"&gt;
  
                  @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);
                  }
  
              &lt;/li&gt;
  
          }
  }
  </code></pre>
  
  <p>also I'm passing model from controller to above partial view this way :</p>
  
  <pre><code>public IList&lt;Category&gt; GetAll()
  {
  
          return _category.Where(category =&gt; category.ParentId == null)
                          .Include(category =&gt; category.Children).ToList();
  }
  public ActionResult Categories()
  {
              var query = GetAll();
              return PartialView("_Categories",query);
  }
  </code></pre>
  
  <p>my Category Model:</p>
  
  <pre><code>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&lt;Category&gt; Children { get; set; }
  
  
          public virtual ICollection&lt;Product&gt; Products { get; set; }
  
   }
  </code></pre>
  
  <p><strong>Updated :</strong><br>
  using recursive helpers are good idea but it doesn't generate sub-menu for me. what's my problem?<br>
  <strong>Any Idea?</strong></p>
  
  <p><strong>Thanks in your advise</strong></p>