I have some viewdata that is generated by going through my repository to the database to grab some scheduling info. When the information is stored in the Viewdata, I noticed that the viewdata is enumerated. How could I access the enumerated items and generate a table/list based on the viewdata? Most of the information just needs to be spit out into a table, but one item will have a link generated for it.
Thanks!
I don’t know really understand what you mean when you say that the viewdata is enumerated. The ViewData contains instances of objects that you put inside your controller action. If you put an instance of an
IEnumerable<T>you could enumerate. So let’s suppose that you store anIEnumerable<ProductViewData>inside your ViewData from the controller action that’s rendering the view:Inside the view you could enumerate and generate a table:
That being said you, I would recommend you to never do this and always use strongly typed views. Using
ViewDatarequires you to cast and use magic strings inside your views which IMHO is bad.Here’s the same using a strongly typed view:
and the view:
and things get even more promising when you start using HTML helpers in MVCContrib such as the Grid: