I have a controller that builds a query from Linq to Sql to go to the ViewBag.products object. The problem is that I cannot use the foreach loop, as I expected that I could.
Here is the code in the controller creating the request using the .ToList () function.
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 > DateTime.Now select new { product.productName, product.productExcerpt, images.imageID, images.imageURL }; ViewBag.products = products.ToList();
Since I am using a different model for index.cshtml for other necessary elements, I thought that a simple Html.Partial could be used to include a viewbag loop. I tried it with the same result using and without using the partial and simple use of foreach in index.cshtml. Below is a snippet that includes a partial:
<div id="bundle_products"> @Html.Partial("_homeBundle") </div>
In my _homeBundle.cshtml file, I have the following:
@foreach (var item in ViewBag.products) { @item }
I get ViewBag data, but I get the whole list as output as such:
{ productName = Awesomenes Game, productExcerpt = <b>Awesome game dude!</b>, imageID = 13, imageURL = HotWallpapers.me - 008.jpg }{ productName = RPG Strategy Game, productExcerpt = <i>Test product excerpt</i>, imageID = 14, imageURL = HotWallpapers.me - 014.jpg }
What I thought I could do is:
@foreach(var item in ViewBag.Products) { @item.productName }
As you can see, in the output is productName = Awesomenes Game. However, I get the error "object" does not contain a definition for "productName" when I try to do this.
How can I output each โfieldโ so as to say individually in my loop so that I can apply the correct HTML tags and style needed for my page?
I need to make a completely new ViewModel for this, and then create a display template as indicated here: 'object' does not contain a definition for 'X'
Or can I do what I'm trying here?
***** ***** UPDATE
In my controller, I now have the following:
var bundle = db.Bundle.Where(a => a.bundleInactiveDate > 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 > DateTime.Now select new { product.productName, product.productExcerpt, images.imageID, images.imageURL }; var bundleContainer = new FullBundleModel(); bundleContainer.bundleItems = bundle; return View(bundleContainer);
I have a model, FullBundleModel
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace JustBundleIt.Models { public class FullBundleModel { public IQueryable<Bundles> bundleItems { get; set; } public IQueryable<Images> imageItems { get; set; } } }
and now my view has
@model IEnumerable<JustBundleIt.Models.FullBundleModel> @foreach (var item in Model) { <div class="hp_bundle"> <h3>@Html.Display(item.bundleName)</h3> </div> }
If I remove the IEnumerable from the model reference, the foreach error excludes that there is no public definition for the counter.
In @ Html.Display (item.bundleName), he makes a mistake that the model does not have a definition for bundleName. If I try to execute
@foreach(var item in Model.bundleItems)
I get an error that bundleItems are not defined in the model.
So, what did I not connect correctly to the combined model?