title: ASP.NET MVC 4 ViewModel
body: <p>Am new to ASP.NET MVC and have been looking to build out the standard ASP.NET MVC template with Identity 2.0 installed from Nuget</p>
  
  <p>I have a College class declared as follows</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace Placementv2.Models
  {
      public class College
      {
          public int CollegeID { get; set; }
          public string Name { get; set; }
          public string Address1 { get; set; }
          public string Address2 { get; set; }
          public string Address3 { get; set; }
          public virtual County County { get; set; }
          public int CountyID { get; set; }
          public string MobilePhone { get; set; }
          public string ContactName { get; set; }
          public string ContactMobilePhone { get; set; }
          public bool CollegeStatus { get; set; }
          public virtual ICollection&lt;Course&gt; Courses { get; set; }
          public double Latitude { get; set; }
          public double Longtitude { get; set; }
          public DateTime LastModified { get; set; }
          public string UserLastModified { get; set; }
  
      }
  }
  </code></pre>
  
  <p>I also have a Course class as follows:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Entity;
  using System.ComponentModel.DataAnnotations;
  namespace Placementv2.Models
  {
      public class Course
      {
          [Key]
          public int CourseID { get; set; }
          public string CourseName { get; set; }
          public int CollegeID { get; set; }
          public bool CourseStatus { get; set; }
          public virtual College College { get; set; }
  
  
      }
  
  }
  </code></pre>
  
  <p>I am then trying to follow the Contoso University ASP.NET MVC5 sample project (see <a href="https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application" rel="nofollow">https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application</a> )to achieve a similar approach taken with instructors and courses where on selecting a college you can drill in to see the specific courses (and their attributes) within the College index view</p>
  
  <p>I then adapted the College Controller</p>
  
  <p>adapting the simple </p>
  
  <pre><code>public ActionResult Index()
          {
              return View(db.Colleges.ToList());
          }
  </code></pre>
  
  <p>and replacing this with </p>
  
  <pre><code>public ActionResult Index(int? id, int? courseID)
  {
      var viewModel = new CollegeIndexData();
  
      viewModel.Colleges= db.Colleges
          .Include(i =&gt; i.Address1)
          .Include(i =&gt; i.Address2)
          .Include(i =&gt; i.Address3)
          .Include(i =&gt; i.County.CountyName)
          .Include(i =&gt; i.CollegeStatus)
          .Include(i =&gt; i.ContactMobilePhone)
          .Include(i =&gt; i.ContactName)
          .Include(i =&gt; i.Name)
          .Include(i =&gt; i.Courses.Select(c =&gt; c.CourseID))
          .OrderBy(i =&gt; i.Name);
  
      if (id != null)
      {
          ViewBag.CollegeID = id.Value;
          viewModel.Courses = viewModel.Colleges.Where(
              i =&gt; i.CollegeID == id.Value).Single().Courses;
      }
      return View(viewModel);
  }
  </code></pre>
  
  <p>Finally, I have updated the Index view of College as follows:</p>
  
  <pre><code>     @model Placementv2.ViewModels.CollegeIndexData
  
  @{
      ViewBag.Title = "Colleges";
  }
  
  &lt;h2&gt;Instructors&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  &lt;table class="table"&gt;
      &lt;tr&gt;
          &lt;th&gt;Name&lt;/th&gt;
          &lt;th&gt;Address1&lt;/th&gt;
          &lt;th&gt;Address2&lt;/th&gt;
          &lt;th&gt;Address3/th&gt;
          &lt;th&gt;County/th&gt;
          &lt;th&gt;Courses&lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
      @foreach (var item in Model.Colleges)
      {
          string selectedRow = "";
          if (item.CollegeID == ViewBag.CollegeID)
          {
              selectedRow = "success";
          }
          &lt;tr class="@selectedRow"&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Address1)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Address2)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Address3)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.County.CountyName)
              &lt;/td&gt;
              &lt;td&gt;
                  @{
          foreach (var course in item.Courses)
          {
              @course.CourseID @:  @course.CourseName&lt;br /&gt;
                      }
                  }
              &lt;/td&gt;
  
              &lt;td&gt;
                  @Html.ActionLink("Select", "Index", new { id = item.CollegeID}) |
                  @Html.ActionLink("Edit", "Edit", new { id = item.CollegeID }) |
                  @Html.ActionLink("Details", "Details", new { id = item.CollegeID }) |
                  @Html.ActionLink("Delete", "Delete", new { id = item.CollegeID })
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  
  &lt;/table&gt;
  
  @if (Model.Courses != null)
  {
      &lt;h3&gt;Courses Offered by Selected College&lt;/h3&gt;
      &lt;table class="table"&gt;
          &lt;tr&gt;
              &lt;th&gt;&lt;/th&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Status&lt;/th&gt;
              &lt;/tr&gt;
  
          @foreach (var item in Model.Courses)
          {
              string selectedRow = "";
              if (item.CourseID == ViewBag.CourseID)
              {
                  selectedRow = "success";
              }
              &lt;tr class="@selectedRow"&gt;
                  &lt;td&gt;
                      @Html.ActionLink("Select", "Index", new { courseID = item.CourseID })
                  &lt;/td&gt;
                  &lt;td&gt;
                      @item.CourseName
                  &lt;/td&gt;
                  &lt;td&gt;
                      @item.CourseStatus
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
  
      &lt;/table&gt;
  }
  }
  </code></pre>
  
  <p>However it throws an exception: </p>
  
  <blockquote>
    <p>A specified Include path is not valid. The EntityType 'IdentitySample.Models.College' does not declare a navigation property with the name 'Address1'.</p>
  </blockquote>
  
  <p>INterestingly (and a little frustratingly, as I am totally confused here!) the College Model does not live in the IdentitySamples namespace but in the Placementv2 namespace.</p>
  
  <p>Could someone help me out here in pointing me in the right direction and also in highlighting suggestions on any other extant code errors in the code samples</p>
  
  <p>Thank you in advance</p>