title: MVC, Passing count of 3 tables to view
body: <p>I'm trying to pass the counts of three different tables into a view along with a list of albums. It's a mod of the MvcMusicStore tutorial. I'm getting the "The ObjectContext instance has been disposed and can no longer be used..." error. I understand that it has to do with lazy loading, but I can't figure out how to make it work.  </p>
  
  <pre><code>// Controller
  public class StoreManagerController : Controller
  {
      private MusicStoreDBEntities context = new MusicStoreDBEntities();  
  
      public ActionResult Index()
      {
          // counts
          ViewData["artistCount"] = context.Artists
                                       .Select(e =&gt; new { e.ArtistId }).Count();
          ViewData["genreCount"] = context.Genres
                                       .Select(e =&gt; new { e.GenreId }).Count();
          ViewData["albumCount"] = context.Albums
                                       .Select(e =&gt; new { e.AlbumId }).Count();
  
          // also tried two other ways...same result. 
          //ViewData["artistCount"] = context.Artists
                                   .Select(e =&gt; new { e.ArtistId }).ToList().Count;
          //ViewData["artistCount"] = context.Artists
                                   .Select(e =&gt; new { e.ArtistId }).ToList().Count();
  
          // album list
          var albums = context.Albums.Include(a =&gt; a.Genre).Include(a =&gt; a.Artist);
          return View(albums.ToList());
      }
  }
  
  
  // View - Index.cshtml  
  @model IEnumerable&lt;MVC_App1.Models.Album&gt;
  &lt;div&gt;
  &lt;label&gt;&lt;strong&gt;Database Counts:&lt;/strong&gt;&lt;/label&gt;
      Albums: @ViewData["albumCount"] ...
      Artists: @ViewData["artistCount"]  ...
      Genres: @ViewData["genreCount"]
  &lt;/div&gt;  
  
  &lt;table&gt;
  @foreach (var item in Model) {
  &lt;tr&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Genre.Name)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Artist.Name)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Title)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Price)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.AlbumArtUrl)
      &lt;/td&gt;
  &lt;/tr&gt;
  }
  &lt;/table&gt;
  </code></pre>
  
  <p>SOLUTION:</p>
  
  <p>So I went with the suggestion to add a new model.  </p>
  
  <pre><code>public class AlbumsCollection 
  {
      public IEnumerable&lt;Album&gt; Albums = new List&lt;Album&gt;();
      public IEnumerable&lt;Artist&gt; Artists = new List&lt;Artist&gt;();
      public IEnumerable&lt;Genre&gt; Genres = new List&lt;Genre&gt;();
  }
  </code></pre>
  
  <p>And then do the counts in the view. I realized that I m passing the whole Album list to the view anyway, so it isn't a slowdown there. And For the Artists and Genres...there won't be many anyway, as they are 1-to-many relationships. I guess that's giving up on doing the Counts in the controller....but I guess this is good enough.</p>