ASP.NET MVC: Accessing ModelMetadata for items in a collection

Posted by DanM on Stack Overflow See other posts from Stack Overflow or by DanM
Published on 2010-03-14T03:19:30Z Indexed on 2010/03/14 3:25 UTC
Read the original article Hit count: 361

I'm trying to write an auto-scaffolder for Index views. I'd like to be able to pass in a collection of models or view-models (e.g., IEnumerable<MyViewModel>) and get back an HTML table that uses the DisplayName attribute for the headings (th elements) and Html.Display(propertyName) for the cells (td elements). Each row should correspond to one item in the collection.

When I'm only displaying a single record, as in a Details view, I use ViewData.ModelMetadata.Properties to obtain the list of properties for a given model. But what happens when the model I pass to the view is a collection of model or view-model objects and not a model or view-model itself?

How do I obtain the ModelMetadata for a particular item in a collection?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2