Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:10:03+00:00 2026-06-11T12:10:03+00:00

I have a controller that is building a query from Linq to Sql to

  • 0

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.

Here’s the code in the controller building the query, with the .ToList() function applied.

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 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:

<div id="bundle_products">
<!--build out individual product icons/descriptions here--->
@Html.Partial("_homeBundle")
</div>

In my _homeBundle.cshtml file I have the following:

@foreach (var item in ViewBag.products)
{ 
    @item
}

I am getting the ViewBag data, but I am getting the entire 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 was:

@foreach(var item in ViewBag.Products)
{
    @item.productName
}

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.

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?

Do I need to make a whole new ViewModel to do this, and then create a display template as referenced here: 'object' does not contain a definition for 'X'

Or can I do what I am attempting 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 my View now 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 IEnumerable from the model reference, the foreach errors out that there is no public definition for an enumerator.

In the @Html.Display(item.bundleName) it errors out that the model has no definition for bundleName. If I attempt

@foreach(var item in Model.bundleItems)

I get an error that bundleItems is not defined in the model.

So what don’t I have wired up correctly to use the combined model?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:10:04+00:00Added an answer on June 11, 2026 at 12:10 pm

    Why not create a new model that contains all of the data that you need?

    Example One:

    public class ContainerModel
    {
        public IQueryable<T> modelOne;
        public IQueryable<T> modelTwo;
    }
    

    This will allow you to access either of your queries in Razor:

    @model SomeNamespace.ContainerModel
    
    @foreach (var item in Model.modelOne)
    {
        //Do stuff
    }
    

    I personally avoid using ViewBag at all and store everything I need in such models because it’s NOT dynamic and forces everything to be strongly typed. I also believe that this gives you a clearly defined structure/intent.

    And just for clarity’s sake:

    public ViewResult Index()
    {
        var queryOne = from p in db.tableOne
                       select p;
    
        var queryTwo = from p in db.tableTwo
                       select p;
    
        var containerModel = new ContainerModel();
        containerModel.modelOne = queryOne;
        containerModel.modelTwo = queryTwo;
    
        return View(containerModel);
    }
    

    Example Two:

    public class ContainerModel
    {
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")] //Format: MM/dd/yyyy (Date)
        public Nullable<DateTime> startDate { get; set; }
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")] //Format: MM/dd/yyyy (Date)
        public Nullable<DateTime> endDate { get; set; }
        public SelectList dropdown { get; set; }
        public IQueryable<T> modelOne { get; set; }
        public IQueryable<T> modelTwo { get; set; }
    }
    

    In this case you’ve stored 3 other items in the model with your 2 queries. You can use the Html helpers to create a Drop Down List in Razor:

    @Html.DropDownList("dropdown", Model.dropdown)
    

    And you can use the DisplayFor helper to display your dates as defined in your model with Data Annotations:

    @Html.DisplayFor(a => a.startDate)
    

    This is advantageous IMO because it allows you to define all of the data that you want to make use of in your View AND how you plan to format that data in a single place. Your Controller contains all of the business logic, your Model contains all of the data/formatting, and your View is only concerned with the content of your page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

--Summary (shortened)-- I have a controller that loads a profile object from the corresponding
I have a bit of a strange problem. I have a controller that is
I have a Controller that pulls images from a database, re-sizes them, caches the
I have a problem that I can't figure out. I'm building a small framework
I am building a blog application. I have a blog controller that is the
I have a controller that looks like this: public class PageController : Controller {
I have a controller that runs some calculation on data, and then I need
I have a controller that contains a tableView that spawns another controller modally to
I have a controller that is called with AJAX (sends JSON data), so I
When you have a controller that does logic with services and DAO's that may

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.