Retrieve property from a dynamic object after storing/retrieving it From the Session

Viewed 846

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

  1. At the debug time, in the watch view I can see the properties/values
  2. I can't store the list in the ViewBag in my case because I used ajax to call the method
  3. I tried to use var, object instead of dynamic => the same result
  4. I think this is not related to MVC or Razor engine
  5. 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 ?

1 Answers
Related