title: How to add values to a list in mvc 4?
body: <p>I have model definition as:</p>
  
  <pre><code>public class CMBCategory
  {
      public int ID { get; set; }
      public string Name { get; set; }
      public List&lt;CMBCategory&gt; children = new List&lt;CMBCategory&gt;();
  }
  </code></pre>
  
  <p>I need to add values to this list , but there may be n number of subitems like</p>
  
  <pre><code>a)Redbook
      a.1) Wedding
            a.1.1) Christian
                .
                .
      a.2)Baptism
  b)Florenta
  </code></pre>
  
  <p>Using the following code I just get only first level of sub products:</p>
  
  <pre><code>foreach (var items in lstcatlist)
  {
      CMBCategory cmb = new CMBCategory();
      cmb.ID = items.FirstOrDefault().catid;
      cmb.Name = items.FirstOrDefault().title;
      foreach (var item1 in items)
      {
          if (item1.parent == 0)
          {
              cmb.children.Add(new CMBCategory() { ID = item1.prod_id, Name = item1.desc });
          }
      }
      lstmain.Add(cmb);
  }
  </code></pre>
  
  <p>Here lstcatlist has the values:
  <a href="https://i.stack.imgur.com/Op90M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Op90M.png" alt="enter image description here"></a>
  It may be because of my loop structure , please provide a way to find the correct structure   to loop n number of sub products if any</p>
  
  <pre><code>var lstcatlist = (from s in context.M_ProductCategory
                    join p in context.M_CategoryDetails on s.ID equals p.CategoryID
                    select new
                    {
                        catid = s.ID,
                        title = s.Title,
                        prod_id = p.ID,
                        parent = p.ParentID,
                        desc = p.Description
                    }).GroupBy(x =&gt; x.catid).ToList();
  </code></pre>