title: How to Display Hierarchial data in MVC Razor view
body: <p>I'm trying to display hierarchial data in my view - I have 2 models (parent/child linked)</p>
  
  <p>Models:</p>
  
  <pre><code>public class Clinic
  {
      public int ClinicId { get; set; }
      [Display(Name="Clinic Name")]
      public string Name { get; set; }
      public string Description { get; set; }
      public List&lt;Property&gt; Properties { get; set; }
  
  }
  
  public class Property
  {
      public int PropertyID { get; set; }
      public int ClinicId { get; set; }
      [Display(Name="Property Address")]
      public string Address { get; set; }
      public Clinic Clinic { get; set; }
      public List&lt;Room&gt; Rooms { get; set; }
   }
  
  public class Room
  {
      public int RoomId { get; set; }
      public int PropertyId { get; set; }
      public string RoomName { get; set; }
      public Property Property { get; set; }
  
  }
  </code></pre>
  
  <p>My controller is builds this Linq query - which displays fine in LinqPad:</p>
  
  <pre><code>    //
      // GET: /Property/Overview/
      public ActionResult Overview()
      {
     // original code - replaced following help below
     //  var ovw =
     //         (from c in db.Clinics
     //          select new
     //          {
     //              c.Name,
     //              Properties =
     //                  from p in db.Properties
     //                  where p.ClinicId == c.ClinicId
     //                  select new
     //                  {
     //                      p.Address
     //                  }
     //                  });
  
          IEnumerable&lt;Clinic&gt; ovw = from c in db.Clinics
          select c;
  
          return View(ovw);
      }
  </code></pre>
  
  <p>My view is:</p>
  
  <pre><code>@model IEnumerable&lt;ttp.Models.Clinic&gt;
  
  @{
      ViewBag.Title = "Overview";
  }
  
  &lt;h2&gt;Overview&lt;/h2&gt;
  
   @foreach (var item in Model) {
   @item.Name
          &lt;br /&gt;
   if (item.Properties != null){
      foreach (var item2 in item.Properties) {
          @item2.Address &lt;br /&gt;
  
   if (item2.Rooms != null){
      foreach (var item3 in item2.Rooms) {
          @item3.RoomName &lt;br /&gt;
       }
  
       }
      }
     }
  </code></pre>
  
  <p>It should display:</p>
  
  <pre><code>Clinic1
     Property1
        Room1
        Room2
        Room3
  
     Property2
        Room4
        Room5
  
  Clinic2
     Property3
        Room6
  ....
  </code></pre>
  
  <p>But instead just shows:</p>
  
  <pre><code>Clinic1
  Clinic2
  Clinic3
  </code></pre>
  
  <p>This will only ever populate the first level of data of Clinics - not the child Properties, or the child Rooms of Properties.</p>
  
  <p>Can anyone please help me fix my controller or my view?</p>
  
  <p>Thanks, Mark</p>