With the ViewBag, I'm able to set random properties, and access them in a View. However, if I create a list of dynamic objects, I am unable to access the properties of those objects. Is there any way to achieve this?
Here is a basic example:
Controller Code:
List<dynamic> Details = new List<dynamic>();
dynamic detail = new {
serviceDate = DateTime.Now.ToString("MM/dd/yyyy"),
Charge = 100.01
};
Details.add(detail);
ViewBag.Details = Details;
return View();
View Code:
@foreach (dynamic detail in ViewBag.Details) {
<li>
@String.Format("{0} - {1}", detail.serviceDate, detail.Charge)
</li>
}
The exception I get is:
'object' does not contain a definition for 'serviceDate'
When debugging the view though, the object clearly has a variable called 'serviceDate'