title: ASP.NET MVC Recursion Procedure
body: <p>I'm trying to extrapolate and transfer some answered questions on recursion into one of my projects. </p>
  
  <p>Answered question on recursion that I'm referencing:<br>
  <a href="https://stackoverflow.com/questions/17553286/asp-net-mvc-4-generating-a-treeview-with-recursive-partial-view">asp-net-mvc-4-generating-a-treeview-with-recursive-partial-view</a></p>
  
  <p>However, I'm having trouble grasping the concept and applying it in my project. </p>
  
  <p>Here is my attempt.</p>
  
  <p>First, I created a class called Models:  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace MVCMnetWebsite.Models
  {
    public class Category
    {
      public int TAB_ITEM_ID { get; set; }
      public string TAB_ITEM_NAME { get; set; }
      public int? TAB_ITEM_PARENT_ID { get; set; }
      public string URL { get; set; }
      public string WINDOW_NAME { get; set; }
      public string TOOL_TIP { get; set; }
      public string ACCESS { get; set; }
      public DateTime? START_DATE { get; set; }
      public DateTime? END_DATE { get; set; }
      public int? LASTUPDATE_BY { get; set; }
      public int? SNAC_TYPES_OBJID { get; set; }
      public byte? ACTIVE { get; set; }
      public string ICON { get; set; }
  }
  
  public class SeededCategories
  {
      public int? Seed { get; set; }
      public IList&lt;Category&gt; Categories { get; set; }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Then, I created a controller called TreeController:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using MVCMnetWebsite.Models;
  
  namespace MVCMnetWebsite.Controllers
  {
    public class TreeController : Controller
    {
      public ActionResult Index()
      {
          var categories = Session["_menu"] as IList&lt;Category&gt;;
          SeededCategories model = new SeededCategories { Seed = null, Categories = categories };
          return View(model);
      }    
    }
  }
  </code></pre>
  
  <p>Then, I created a partial view called _TreeCategories:</p>
  
  <pre><code>@model MVCMnetWebsite.Models.SeededCategories
  @using MVCMnetWebsite.Models
  @{var menuList = Session["_menu"] as List&lt;Category&gt;;}
  @{int appID = 72;} @*PKEY OF APP - LOCATED: [INTRAWEB].[dbo][DATA_SN_ACCESS_CONTROL_TYPES]*@
  
  @foreach (var topMenuList in menuList.Where(x =&gt; x.TAB_ITEM_PARENT_ID == null &amp;&amp; x.SNAC_TYPES_OBJID == appID).Select(x =&gt; x).ToList())
  {
  &lt;li&gt;
      &lt;a href="#"&gt;&lt;span&gt;&lt;i class="@topMenuList.ICON"&gt;&lt;/i&gt;  @topMenuList.TAB_ITEM_NAME&lt;/span&gt;&lt;/a&gt;
      &lt;ul&gt;
          @foreach (var subMenuList in menuList.OrderBy(x =&gt; x.TAB_ITEM_NAME).Where(x =&gt; x.TAB_ITEM_PARENT_ID == topMenuList.TAB_ITEM_ID).Select(x =&gt; x).ToList())
          {
              if (menuList.Any(x =&gt; x.TAB_ITEM_PARENT_ID == subMenuList.TAB_ITEM_ID))
              {
                  &lt;li&gt;
                      &lt;a href="#"&gt;&lt;span&gt;&lt;i class="fa fa-toggle-right"&gt;&lt;/i&gt; @subMenuList.TAB_ITEM_NAME&lt;/span&gt;&lt;/a&gt;
                      &lt;ul&gt;
                          @foreach (var subChildMenuList in menuList.Where(x =&gt; x.TAB_ITEM_PARENT_ID == subMenuList.TAB_ITEM_ID).Select(x =&gt; x).ToList())
                          {
                              &lt;li class="urlLink"&gt;
                                  &lt;a name="@subChildMenuList.URL" href=""&gt;@subChildMenuList.TAB_ITEM_NAME&lt;/a&gt;
                              &lt;/li&gt;
                          }
                      &lt;/ul&gt;
                  &lt;/li&gt;
              }
              else
              {
                  &lt;li class="urlLink"&gt;&lt;a name="@subMenuList.URL" href=""&gt;@subMenuList.TAB_ITEM_NAME &lt;/a&gt;&lt;/li&gt;
              }
          }
      &lt;/ul&gt;
  &lt;/li&gt;
  }
  </code></pre>
  
  <p>and in my _layout.cshtml, I have:</p>
  
  <pre><code>@Html.Partial("_TreeCategories", Model)
  </code></pre>
  
  <p>From the code, shown above, I can only show items up to the 2nd child. How do I use recursion to show items up to the Nth child?</p>
  
  <p>EDITED:</p>
  
  <pre><code>@if (Model.Categories.Where(s =&gt; s.TAB_ITEM_PARENT_ID == Model.Seed).Any())
  {
    &lt;ul&gt;
      @foreach (var node in Model.Categories)
      {
          if (node.TAB_ITEM_PARENT_ID == Model.Seed)
          {
              SeededCategories inner = new SeededCategories { Seed = node.TAB_ITEM_ID, Categories = Model.Categories };
              &lt;li&gt;
                  &lt;a href="?@node.TAB_ITEM_ID"&gt;@node.TAB_ITEM_NAME&lt;/a&gt;
                  @Html.Partial("_TreeCategories", inner)
              &lt;/li&gt;
          }
      }
  &lt;/ul&gt;
  }
  </code></pre>
  
  <p>The above code works but it needs to be tweaked in order to render the proper style. Additionally, the code above was taken from the link mentioned at the top. I just dont know where to add my HTML structure - meaning: The above code starts with an unordered list where mine starts with a list item The HTML is different and I don't see the pattern and how to transfer this to my situation. </p>