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?
You are using an anonymous object here:
Anonymous objects are emitted as
internalby 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:
and then in your controller use this view model:
And now that you have a view model the next improvement to your code is of course to get rid of this crap of
ViewBagthat I am sick of seeing and simply use view models and strong typing:and then of course have a strongly typed view:
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):