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 6840491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:51:03+00:00 2026-05-26T23:51:03+00:00

I have a list in the controller as follows: public class TruckData { public

  • 0

I have a list in the controller as follows:

    public class TruckData
    {
       public string Truck{ get; set; }                        
    }


     var truckdat = (from p in KowaDataContext.tblTrucks
                     orderby p.Truck
                     select new TruckData {Truck= p.Truck});


     var trucklist = truckdat.ToList();

     ViewBag.TrckList= trucklist;

The View looks like the following:

    @foreach (Data.Rep.Controllers.TruckController.TruvckData item in ViewBag.TrckList)         
    {
     <tr>        

      <td>
         @item.Truck
      </td>
   </tr>      

   }

My question is, how can I pass the list to the view without using the class:

     public class TruckData
    {
         public string Truck{ get; set; }                        
    }

so that my list looks like the following: Notice how the reference to TruckData is gone below:

    var truckdat = (from p in KowaDataContext.tblTrucks
                       orderby p.Truck
                       select new {Truck= p.Truck});

     var trucklist = truckdat.ToList();

     ViewBag.TrckList= trucklist;

I am not sure how the foreach in the view will look like. I tried what is shown below but was giving me a problem.

  @foreach (var item in ViewBag.TrucksSelectList) 
  • 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-05-26T23:51:04+00:00Added an answer on May 26, 2026 at 11:51 pm

    No, this is not possible. You cannot pass anonymous objects to views. The reason for this is that anonymous objects are emitted as internal by the compiler. And since ASP.NET dynamically compiles views into a separate assembly they do not have access to this model.

    My question is, how can I pass the list to the view without using the class:

    That would be against all good practices => you need to use a view model:

    var truckdat = 
      from p in KowaDataContext.tblTrucks
      orderby p.Truck
      select new TruckData { Truck = p.Truck };
    
    ViewBag.TrckList = truckdat.ToList();
    

    and then in the view:

    @foreach (var item in (IEnumerable<TruckData>)ViewBag.TrckList) 
    {
        <div>@item.Truck</div>
    }
    

    And since you have a list of strings you could probably use that as model:

    var truckdat = 
      from p in KowaDataContext.tblTrucks
      orderby p.Truck
      select p.Truck;
    
    ViewBag.TrckList = truckdat.ToList();
    

    and then in the view:

    @foreach (var item in (IEnumerable<string>)ViewBag.TrckList) 
    {
        <div>@item</div>
    }
    

    I would also very strongly recommend you to get rid of ViewBag and use strongly typed view models:

    public ActionResult Foo()
    {
        IEnumerable<TruckData> model = 
            from p in KowaDataContext.tblTrucks
            orderby p.Truck
            select new TruckData { Truck = p.Truck };
    
        return View(model);
    }
    

    and then:

    @model IEnumerable<TruckData>
    @foreach (var item in Model)
    {
        <div>@item.Truck</div>
    }
    

    And introducing a display template for the TruckData type (~/Views/Shared/DisplayTemplates/TruckData.cshtml):

    @model TruckData
    <div>@Model</div>
    

    you can even get rid of the ugly and useless foreach loop in your main view:

    @model IEnumerable<TruckData>
    @Html.DisplayForModel()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have some view models set up as follows: public class User
I have designed my jqgrid viewmodel as follows, public class DomainJQGrid { public JQGrid
So I have a controller set up as follows: using NonStockSystem.Models; namespace NonStockSystem.Controllers {
Let's say I have a simple ASP MVC3 list controller, with an add method,
so i have a Parents controller with list and edit view for it (to
I have controller PlayerController and actions inside: View , Info , List . So
I have a very simple RootView Controller ->Detail View Controller, to display a list
If I have a simple controller routed as follows: context.MapRoute( Default, {controller}/{action}, new {
I have a model like this defined... public class Product{ private long timestamp; //
I have a link as follows. @Html.ActionLink(Create Report, Screenreport, Reports, null, new { @class

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.