In my MVC Application:
In the controller I created a list of dynamic type, which is stored in the session.
The view then attempts to access the objects but it throws an exception : 'object' does not contain a definition for 'a'
The code :
// Controller
List<dynamic> myLists = new List<dynamic>();
for(int i=0; i<3; i++){
var ano = new { a='a' , b = i };
myLists.Add(ano);
}
Session["rows"] = myLists;
In my view
// My View
foreach( dynamic row in (Session["rows"] as List<dynamic>)){
<div>@row</div> // output {a:'a', b :1}
<div>@row.a</div> // throw the error.
}
Note
- At the debug time, in the watch view I can see the properties/values
- I can't store the list in the ViewBag in my case because I used ajax to call the method
- I tried to use
var
,object
instead ofdynamic
=> the same result - I think this is not related to MVC or
Razor engine
- I tried to use a aspx view (not the razor one) and the same result
Why I can't access the property, if the debugger can see it, and how can I solve this problem ?