title: Foreach ViewBag data gives 'object' does not contain a definition for 'var'
body: <p>I have a controller that is building a query from Linq to Sql to pass into the ViewBag.products object.  The problem is, I cannot loop using a foreach on it as I expected I could.</p>
  
  <p>Here's the code in the controller building the query, with the .ToList() function applied.</p>
  
  <pre><code>var products = from bundles in db.Bundle
      join bProducts in db.BundleProducts on bundles.bundleId equals bProducts.bundleID
      join product in db.Products on bProducts.productID equals product.productID
      join images in db.Images on product.productID equals images.productID
      where bundles.bundleInactiveDate &gt; DateTime.Now
            select new {
                product.productName,
                product.productExcerpt,
                images.imageID,
                images.imageURL
             };
  ViewBag.products = products.ToList();
  </code></pre>
  
  <p>Since I am using a different model on the Index.cshtml for other items needed, I thought a simple Html.Partial could be used to include the viewbag loop.  I have tried it with the same result with and without using the partial and simply by using the foreach in the index.cshtml.  A snippet that includes the partial is below:</p>
  
  <pre><code>&lt;div id="bundle_products"&gt;
  &lt;!--build out individual product icons/descriptions here---&gt;
  @Html.Partial("_homeBundle")
  &lt;/div&gt;
  </code></pre>
  
  <p>In my _homeBundle.cshtml file I have the following:</p>
  
  <pre><code>@foreach (var item in ViewBag.products)
  { 
      @item
  }
  </code></pre>
  
  <p>I am getting the ViewBag data, but I am getting the entire list as output as such:</p>
  
  <pre><code>{ productName = Awesomenes Game, productExcerpt = &lt;b&gt;Awesome game dude!&lt;/b&gt;, imageID = 13, imageURL = HotWallpapers.me - 008.jpg }{ productName = RPG Strategy Game, productExcerpt = &lt;i&gt;Test product excerpt&lt;/i&gt;, imageID = 14, imageURL = HotWallpapers.me - 014.jpg }
  </code></pre>
  
  <p>What I thought I could do was:</p>
  
  <pre><code>@foreach(var item in ViewBag.Products)
  {
      @item.productName
  }
  </code></pre>
  
  <p>As you can see, in the output, productName = Awesomenes Game.  However, I get the error 'object' does not contain a definition for 'productName' when I attempt this.</p>
  
  <p>How can I output each "field" so to say individually in my loop so I can apply the proper HTML tags and styling necessary for my page?</p>
  
  <p>Do I need to make a whole new ViewModel to do this, and then create a display template as referenced here: <a href="https://stackoverflow.com/questions/7652749/object-does-not-contain-a-definition-for-x">&#39;object&#39; does not contain a definition for &#39;X&#39;</a></p>
  
  <p>Or can I do what I am attempting here?</p>
  
  <p>*****UPDATE*****</p>
  
  <p>In my Controller I now have the following:</p>
  
  <pre><code>        var bundle = db.Bundle.Where(a =&gt; a.bundleInactiveDate &gt; DateTime.Now);
          var products = from bundles in db.Bundle
                         join bProducts in db.BundleProducts on bundles.bundleId equals bProducts.bundleID
                         join product in db.Products on bProducts.productID equals product.productID
                         join images in db.Images on product.productID equals images.productID
                         where bundles.bundleInactiveDate &gt; DateTime.Now
                         select new {
                             product.productName,
                             product.productExcerpt,
                             images.imageID,
                             images.imageURL
                         };
          var bundleContainer = new FullBundleModel();
  
          bundleContainer.bundleItems = bundle;
  
          return View(bundleContainer);
  </code></pre>
  
  <p>I have a model, FullBundleModel</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace JustBundleIt.Models
  {
      public class FullBundleModel
      {
          public IQueryable&lt;Bundles&gt; bundleItems { get; set; }
          public IQueryable&lt;Images&gt; imageItems { get; set; }
      }
  }
  </code></pre>
  
  <p>and my View now has</p>
  
  <pre><code>@model IEnumerable&lt;JustBundleIt.Models.FullBundleModel&gt;
  
  @foreach (var item in Model) 
  {
  &lt;div class="hp_bundle"&gt;
  
      &lt;h3&gt;@Html.Display(item.bundleName)&lt;/h3&gt;
      &lt;/div&gt;
  }       
  </code></pre>
  
  <p>If I remove IEnumerable from the model reference, the foreach errors out that there is no public definition for an enumerator.</p>
  
  <p>In the @Html.Display(item.bundleName) it errors out that the model has no definition for bundleName.  If I attempt </p>
  
  <pre><code>@foreach(var item in Model.bundleItems)
  </code></pre>
  
  <p>I get an error that bundleItems is not defined in the model.</p>
  
  <p>So what don't I have wired up correctly to use the combined model?</p>