title: .net mvc 3 passing related classes from the controller
body: <p>God my head is killing me.</p>
  
  <p>Ok I have a controller where i want to pass nested data to the view. Which I am doing like so:</p>
  
  <pre><code>namespace helpme.mvc.Controllers
  { 
    public class CategoryController : Controller
    {
      private HelpMeContext db = new HelpMeContext();
  
      public ViewResult Index()
      {
          var model = db.Category.Include(c =&gt; c.SubCategories).ToList();
  
          return View(model);
      }
    }
  }
  </code></pre>
  
  <p>But it is not working. SubCategories come out empty even though there are rows in them. Any suggestions?</p>
  
  <p>Using a break point i see that the model is being correctly populated, but nothing is displayed in the view and no error msg.</p>
  
  <p>Here is the view code:</p>
  
  <pre><code>@model IEnumerable&lt;helpme.mvc.Models.Category&gt;
  
  @{
      ViewBag.Title = "Index";
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  
  @Model.First().SubCategories.First().Name // UPDATE, THIS DISPLAYS A VALUE, SO WHY DOES THE CODE BELOW JUST DISPLAY THE FIRST LEVEL (Categories)?
  
  &lt;ul&gt;
  @foreach (var c in Model) {
      &lt;li&gt;
              &lt;ul&gt;
              @foreach (var sc in c.SubCategories)
              {
                  @Html.Display(sc.Name)
  
                  foreach (var ssc in sc.SubSubCategories)
                   {
                       @Html.Display(ssc.Name)
                   }
              }
              &lt;/ul&gt;
      &lt;/li&gt;
  }
  &lt;/ul&gt;
  </code></pre>
  
  <p>For some reason it only displays the first level, as if it did not receive the SubCategories, even though the break-point proves that it did.</p>
  
  <p>And here is the model, just for reference:</p>
  
  <pre><code>public class Category
  {
      public Category()
      {
          SubCategories = new List&lt;SubCategory&gt;();
      }
  
      public int ID { get; set; }
  
      [StringLength(255, MinimumLength = 1)]
      public string Name { get; set; }
  
      public ICollection&lt;SubCategory&gt; SubCategories { get; set; }
  }
  
  public class SubCategory
  {
      public SubCategory()
      {
          SubSubCategories = new List&lt;SubSubCategory&gt;();
      }
  
      public int ID { get; set; }
  
      [Required()]
      [StringLength(255, MinimumLength = 1)]
      public string Name { get; set; }
  
      public Category Category { get; set; }
      public ICollection&lt;SubSubCategory&gt; SubSubCategories { get; set; }
  }
  </code></pre>