0

In my projects, I frequently use viewmodels (simple classes used just for transferring data to view) like ProductAdditionnalInformationForCustomFees classes who are used just to render data and used in few times in the project.

But, actually I have a lot of these and I think it's not the best way to handle data transfer between controller and view.

I could use anonymous classes but i would lost the properties intellisense who greatly improve development time and bug hunting.

Is there a way to make something like anonymous classes but with intellisense?

Cœur
  • 37,241
  • 25
  • 195
  • 267
eka808
  • 2,257
  • 3
  • 29
  • 41
  • Why is it not the best way to handle data transfer beween controller and view? You don't need viewmodels for every view, sometimes you can just pass the model if thats all you need. – Steve's a D Dec 05 '12 at 14:44
  • Yep but most of the time I need these kind of classes to transfer to the view what I Need. Like for a client, i would need the client entity, the adress entity etc... – eka808 Dec 05 '12 at 15:00
  • 1
    Anonymous classes *do* have intellisense. They were also specifically designed to be used within the context of a single method and not to be passed to other methods. While it's *possible* to do so, it can't be done effectively, and one side effect is usually losing intellisense (among other problems). It's *much* easier to create a named class than to deal with those problems, especially when it comes to maintenance. – Servy Dec 05 '12 at 15:26
  • Okay so there is no elegant way to make what i want ? – eka808 Dec 05 '12 at 15:53
  • 1
    No.. because what you want.. is not elegant. – BenjaminPaul Dec 05 '12 at 16:47
  • I think having hundred of classes like i do is not elegant neither... – eka808 Dec 06 '12 at 09:36

1 Answers1

1

Anonymous classes are marked Internal. This makes it difficult to share between your view and controller. There are workarounds but stick with the strongly typed classes.

One possible and interesting workaround but you won't get intellisense: Dynamic Anonymous type in Razor causes RuntimeBinderException

Community
  • 1
  • 1
andleer
  • 22,388
  • 8
  • 62
  • 82