title: Recursion in an ASP.NET MVC view
body: <p>I have a nested data object for a set of items within categories. Each category can contain sub categories and there is no set limit to the depth of sub categories. (A file system would have a similar structure.) It looks something like this:</p>
  
  <pre><code>class category
  {
      public int id;
      public string name;
      public IQueryable&lt;category&gt; categories;
      public IQueryable&lt;item&gt; items;
  }
  class item
  {
      public int id;
      public string name;
  }
  </code></pre>
  
  <p>I am passing a list of categories to my view as <code>IQueryable&lt;category&gt;</code>. I want to output the categories as a set of nested unordered list (<code>&lt;ul&gt;</code>) blocks. I could nest foreach loops, but then the depth of sub categories would be limited by the number of nested foreach blocks. In WinForms, I have done similar processing using recursion to populate a <code>TreeView</code>, but I haven't seen any examples of using recursion within an ASPX MVC view.</p>
  
  <p>Can recursion be done within an ASPX view? Are there other view engines that include recursion for view output?</p>