With ASP .NET MVC3.
In my controller I have this portion of code
MasterMindDnetEntities context = new MasterMindDnetEntities();
List<MasterMindDnet.Games> Games = (from g in context.Games
join u in context.Users on g.g_u_Id equals u.u_Id
where u.u_Login == User.Identity.Name
select g).ToList();
@ViewBag.Games = Games;
@ViewBag.GameCount = Games.Count;
return View("Index");
In the controll, Games works fine as a list.
In my view I have:
@for (int i = 0; i < ViewBag.GameCount; i++)
{
ViewBag.Games = ViewBag.Games[i];
At line ViewBag.Games = ViewBag.Games[i];, I get the exception :
Cannot apply indexing with [] to an expression of type
‘System.Data.Entity.DynamicProxies.
In a previous version I had i < ViewBag.Games.Count; instead of i < ViewBag.GameCount; and it said that ViewBag.Games does not have a definition for Count.
How could I solve this ?
Thank you for your help.
Problem of dynamic proxies mean that you have to declare “statically” what’s in your ViewBag.
So in your View, just make :