title: ASP.NET MVC - Displaying a list of items which each have a list of items
body: <p>I hope this is the best way of explaining this...</p>
  
  <p>I have 3 view objects: School, Courses, and Classes.  Each school has multiple courses, and each course can have multiple classes (think of a course as a program of study, with classes being the actual classes).  In my main View, I display all schools, and click one to go to it.  On that "CourseView" page, it displays the name of the school as well as all courses associated with that school.  What I'm trying to do, is also have all classes associated with each course listed also.  Is this possible (without a ton of jQuery/JSON wizardry, which I am still learning)?</p>
  
  <pre><code>public class School
  {
      public int Id { get; set; }
      public string SchoolName { get; set; }
      public string WelcomeMsg { get; set; }
      public string SchoolLogo { get; set; }
      public List&lt;Course&gt; Courses { get; set; }
  }
  
  public class Course
  {
      public int Id { get; set; }
      public string CourseCode { get; set; }
      public string CourseName { get; set; }
      public int SchoolId { get; set; }
      public List&lt;Class&gt; Classes { get; set; }
  }
  
  public class Class
  {
      public int Id { get; set; }
      public string ClassNumer { get; set; }
      public int CourseId { get; set; }
      public int InstructorId { get; set; }
  }
  </code></pre>
  
  <p>In my SchoolDAO.cs, I have the following:</p>
  
  <pre><code>                {
                      cmd.CommandType = CommandType.StoredProcedure;
                      cmd.Parameters.AddWithValue("@SchoolId", SchoolId);
                      using (SqlDataReader dr = cmd.ExecuteReader())
                      {
                          if (dr.Read())
                          {
                              // fill school object in another method.
                              school = readRecord(dr);
                          }
                      }
  
                      // Get all courses available for this school.
                      school.Courses = CoursesDAO.GetCourses(SchoolId);
  
                      return school;
                  }
  </code></pre>
  
  <p>Which also calls a method in the CourseDAO.cs to get the courses available at that school:</p>
  
  <pre><code>            {
                  SqlCommand cmd = new SqlCommand("Courses.GetCourses", conn);
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.Parameters.AddWithValue("@SchoolId", SchoolId);
                  using (SqlDataReader dr = cmd.ExecuteReader())
                  {
                      while (dr.Read())
                      {
                          courses.Add(readRecord(dr));
                      }
                  }
  
                  // Get all classes available for each course.
                  foreach (var course in courses)
                  {
                      course.Classes = ClassDAO.GetClasses(course.Id);
                  }
  
                  return courses;
              }
  </code></pre>
  
  <p>Which then calls the ClassDAO method to get all classes for each course:</p>
  
  <pre><code>            {
                  SqlCommand cmd = new SqlCommand("Courses.GetCourses", conn);
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.Parameters.AddWithValue("@SchoolId", courseId);
                  using (SqlDataReader dr = cmd.ExecuteReader())
                  {
                      while (dr.Read())
                      {
                          classes.Add(readRecord(dr));
                      }
                  }
  
                  return classes;
              }
  </code></pre>
  
  <p>So finally, my question is this:  How do I display the list of schools, courses, and classes in a View, from the Model?</p>
  
  <p>My View has the following, which displays which school I'm looking at, and the list of courses for that school:</p>
  
  <pre><code>&lt;div class="display-field"&gt;
      &lt;h1&gt;&lt;%: Model.SchoolName %&gt;&lt;/h1&gt;
      &lt;h3&gt;&lt;%: Model.WelcomeTitle %&gt;&lt;/h3&gt;
      &lt;blockquote&gt; &lt;i&gt;&lt;%: Model.WelcomeText %&gt;&lt;/i&gt;&lt;/blockquote&gt; 
  &lt;/div&gt;
  
  &lt;!-- Courses --&gt;
  &lt;div class="display-field"&gt;
      &lt;table id="courses"&gt;             
              &lt;thead&gt;
                  &lt;th&gt;Course Code&lt;/th&gt;
                  &lt;th&gt;Course Name&lt;/th&gt;
              &lt;/thead&gt;
          &lt;tbody&gt;
          &lt;%
              foreach (var item in Model.Courses) {
          %&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;%: Html.DisplayFor(model =&gt; item.CourseCode) %&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%: Html.DisplayFor(model =&gt; item.CourseName) %&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;% } %&gt;&lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>After that table, I wanted to put another table with id="classes", but sadly when I try to do:</p>
  
  <pre><code>        &lt;%
              foreach (var class in Model.Courses) {
          %&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;%: Html.DisplayFor(model =&gt; item.ClassNumber) %&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;% } %&gt;
  </code></pre>
  
  <p>It won't work - I'm assuming because I either don't know how to access a list belonging to a list, or because that's not the way the Model works.</p>
  
  <p>Any insight into this would be appreciated greatly.  Thanks!</p>
  
  <p>PS - The information is passed from the DAO -> Business -> Controller by simple methods such as:</p>
  
  <p>SchoolController:</p>
  
  <pre><code>    public ActionResult ViewSchool(int Id)
      {
          School school = SchoolBusiness.GetSchool(Id);
          return View(school);
      }
  </code></pre>
  
  <p>SchoolBusiness.GetSchool:</p>
  
  <pre><code>    public static School GetSchool(int Id)
      {
          School school = SchoolDAO.GetSchool(Id);
          return school;
      }
  </code></pre>