Support dynamic/anonymous types in views
Allow a view to manipulate anonymous models like new View(new {Name="John", Age=20}) and in cshtml file can access this properties like @Model.Age @Model.Name
3 comments
-
Michael Paterson commented
You can use dynamic as your Model: @model dynamic
-
Vasile Bujac
commented
This would be helpful when doing projections with Linq to Entity or Linq to NHibernate and passing anonymous objects directly to the view. The disadvantage is that you loose all the strong-typing, but you don't have to create many viewmodel classes. Sometimes these are numerous.
Currently anonymous types are internal, and razor views are compiled as separate assemblies so dynamic doesn't work. Maybe a change to the DLR which would allow some of these anonymous types to be used with dynamic (annotated with an attribute maybe). -
crpietschmann
commented
Isn't this what the new ViewBag dynamic object in MVC 3 is for?