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

  • Home
  • SEARCH
  • 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 6655555
In Process

The Archive Base Latest Questions

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

I had this problem once before and didn’t resolve it. I have a list

  • 0

I had this problem once before and didn’t resolve it. I have a list (generated in an MVC3 controller):

ViewBag.Languages = db.Languages
    .Select(x => new { x.Name, x.EnglishName, x.Id })
    .ToList();

and on my page (Razor) I try to iterate through it:

foreach (var o in ViewBag.Languages)
{
    string img = "Lang/" + o.EnglishName + ".png";
    @* work *@
}

but the reference to o.EnglishName fails with the error:

‘object’ does not contain a definition for ‘EnglishName’

though the curious thing is that if I type into the Immediate Window (whilst debugging):

o
{ Name = བོད་སྐད་, EnglishName = Tibetan, Id = 31 }
    EnglishName: "Tibetan"
    Id: 31
    Name: "བོད་སྐད་"

so obviously the field is there. What is my problem here?

  • 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-26T01:32:03+00:00Added an answer on May 26, 2026 at 1:32 am

    You are using an anonymous object here:

    ViewBag.Languages = db.Languages
        .Select(x => new { x.Name, x.EnglishName, x.Id })
        .ToList();
    

    Anonymous objects are emitted as internal by the compiler. The Razor views are automatically compiled into a separate assembly by the ASP.NET runtime. This means that you cannot access any anonymous objects generated in your controllers.

    So in order to fix your issue you could define a view model:

    public class LanguageViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string EnglishName { get; set; }
    }
    

    and then in your controller use this view model:

    ViewBag.Languages = db.Languages
        .Select(x => new LanguageViewModel
        { 
            Name = x.Name, 
            EnglishName = x.EnglishName, 
            Id = x.Id 
         })
        .ToList();
    

    And now that you have a view model the next improvement to your code is of course to get rid of this crap of ViewBag that I am sick of seeing and simply use view models and strong typing:

    public ActionResult Foo()
    {
        var model = db
            .Languages
            .Select(x => new LanguageViewModel
            { 
                Name = x.Name, 
                EnglishName = x.EnglishName, 
                Id = x.Id 
            })
            .ToList();
        return View(model);
    }
    

    and then of course have a strongly typed view:

    @model IEnumerable<LanguageViewModel>
    @Html.DisplayForModel()
    

    and then define the corresponding display template which will automatically be rendered by the ASP.NET MVC engine for each element of the view model so that you don’t even need to write a single foreach in your views (~/Views/Shared/DisplayTemplates/LanguageViewModel.cshtml):

    @model LanguageViewModel
    ... generate the image or whatever you was attempting to do in the first place
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I have this problem that I've never had before, it's really bugging me.
I had this problem before and can't for life of me remember how to
I've had this problem many times before, and I've never had a solution I
I know I've had this problem before so I'm really frustrated. I've got the
I have had this problem w/ two seperate WYSWYG editors in my rails application
I had this problem some time ago and I gave up but lately it
I've had this problem a couple of times. Let's say I want to display
Has anyone had this problem? I deleted a column in the table editor and
Just wondering if you had any thoughts on this problem. I want to make
Need some help with this problem in implementing with XSLT, I had already implemented

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.