1

The main purpose that I use anonymous class is to pass data containing various values outside the main model to view without having to create a separate viewmodel.

I found myself very comfortable with LINQ and ExpandoObject. However, since I marked the view with @model IEnumerable, I was no longer able to use Html Helper because its methods should be resolved as extension methods, which was not allowed and should throw a Runtime Exception.

@Html.Display("Property")

The problem is that I have properties needing rendering as TextBox etc. for editing thus making it almost compulsory for me to make use of Html Helper methods like EditorFor(). Therefore, I think I might as well return to strongly typed models.

However, I really don't want to mess my project up with countless "temporary" viewmodels which are just a mixture of existing models. So is there any way to accomplish that? I'm wondering if there is any better solution than a composite class.

tereško
  • 58,060
  • 25
  • 98
  • 150
Isilmë O.
  • 1,668
  • 3
  • 23
  • 35
  • Sounds like you want to use `ViewData` instead of _anonymous models_. Not sure what you're trying to do though. – Leri Aug 20 '13 at 11:26
  • I dunno, since you can't bind dynamic object properties in expressions ("error CS1963: An expression tree may not contain a dynamic operation "), I would suggest that you just create a set of custom ViewModels, with common pieces to keep it as DRY as possible. Plus with custom ViewModels, you can get compile-time checking of your Views for syntax errors. – Graham Aug 20 '13 at 12:30
  • Have a lool at this answer http://stackoverflow.com/questions/6612938/razor-view-with-anonymous-type-model-class-it-is-possible?rq=1 – GraemeMiller May 05 '14 at 17:58

1 Answers1

0

You can use System.Reflection to get the class Properties names. An example: typeof(object).GetProperties

ugo
  • 2,705
  • 2
  • 30
  • 34
Mohamed Hashem
  • 146
  • 2
  • 2