Hadi Hariri

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

Dynamic types and ASP.NET MVC

If you’re using ViewModels in your ASP.NET MVC applications, know that if working with .NET 4.0, you can now create dynamic view models. And it’s actually very simple to do.

Create an ExpandoObject to represent your ViewModel:

 

image

Declare your view to be of type ViewPage<dynamic>

 

image

 

 

 

And you’re done. Here’s the output:

 

image

 

If you’re using ReSharper, you will get Intellisense once you’ve declared a property once (both in the Action as well as the View).


Posted 12-30-2009 1:29 PM by Hadi Hariri
Filed under:

[Advertisement]

Comments

Ben wrote re: Dynamic types and ASP.NET MVC
on 12-30-2009 8:22 AM

What is the benefit of this over an anonymous type?

Hadi Hariri wrote re: Dynamic types and ASP.NET MVC
on 12-30-2009 8:40 AM

@Ben,

Just as there's not that much difference in using ViewData. Maybe a little bit more descriptive and partially having Intellisense. I've always advocated strongly-typed models and have not used the dynamic approach in anger, so I couldn't really tell you how well it works in reality, but I'm hoping to try it out soon.

Twitter Mirror wrote "Dynamic types and ASP.NET MVC" - http://devlicio.us/blogs/hadi_hariri/archive/2009/12/30/dynamic-types-and-asp-net-mvc.aspx pretty neat
on 12-30-2009 9:00 AM

"Dynamic types and ASP.NET MVC" - devlicio.us/.../dynamic

Rik Hemsley wrote re: Dynamic types and ASP.NET MVC
on 12-30-2009 9:51 AM

What's the advantage over strongly typed view models?

Hadi Hariri wrote re: Dynamic types and ASP.NET MVC
on 12-30-2009 10:08 AM

@Rik,

In essence, that you wouldn't need to declare it.

Rik Hemsley wrote re: Dynamic types and ASP.NET MVC
on 12-30-2009 11:03 AM

I'm not sure that having to declare a type is a major pain. Certainly not enough to lose static typing. Especially when R# will build your view model class on the fly with a few presses of alt-return, return. Doubly especially when there's no actual benefit, only drawbacks.

Paco wrote re: Dynamic types and ASP.NET MVC
on 12-30-2009 11:20 AM

I don't understand why dynamic is used for such a thing. This has nothing to do with interaction with dynamic languages.

Hadi Hariri wrote re: Dynamic types and ASP.NET MVC
on 12-30-2009 12:16 PM

@Rik, @Paco

As I pointed out to @Ben, I've been an advocate of strongly-typed models and in this post I'm merely pointing out how do use dynamic types in views. If someone is ViewData, this is just another step for less friction.

I've not taken this approach in a real project to be able to value the trade-offs yet

Arnis L. wrote re: Dynamic types and ASP.NET MVC
on 12-30-2009 3:46 PM

This looks nice and clean, but what about templates? Where would metadata go? How would you specify that stuff using dynamic?

Twitter Mirror wrote @bellware: http://devlicious.com/blogs/hadi_hariri/archive/2009/12/30/dynamic-types-and-asp-net-mvc.aspx
on 12-31-2009 5:53 AM
Twitter Mirror wrote Wow: Dynamic types & ASP.NET MVC http://devlicio.us/blogs/hadi_hariri/archive/2009/12/30/dynamic-types-and-asp-net-mvc.aspx
on 01-03-2010 1:51 PM

Wow: Dynamic types & ASP.NET MVC devlicio.us/.../dynamic

Code Monkey Labs wrote Weekly Web Nuggets #83
on 01-06-2010 4:33 PM

Pick of the week: Responsible Open Source Code Parenting General Micro Optimization Decision Process : Oren Eini walks us through his process of identifying and addressing micro optimizations . Thoughts On EF vs. NHibernate Redux : Rob Conery shares his

Peter wrote re: Dynamic types and ASP.NET MVC
on 03-07-2010 8:19 AM

Looks great. I think it's usable for scenario's where you have dynamic formcontent and using this to create a dynamic model could be very handy.

devlicio.us wrote re: Dynamic types and ASP.NET MVC
on 05-11-2011 6:10 PM

Dynamic types and asp net mvc.. Nice :)

devlicio.us wrote re: Dynamic types and ASP.NET MVC
on 06-03-2011 4:18 AM

Dynamic types and asp net mvc.. OMG! :)

devlicio.us wrote re: Dynamic types and ASP.NET MVC
on 06-05-2011 4:35 AM

Dynamic types and asp net mvc.. Keen :)

devlicio.us wrote re: Dynamic types and ASP.NET MVC
on 06-25-2011 6:49 PM

Dynamic types and asp net mvc.. Amazing :)

hbkq xxx f5zt wrote re: Dynamic types and ASP.NET MVC
on 07-02-2011 10:24 AM

Dynamic types and asp net mvc.. WTF? :)

1gph porno dfd9 wrote re: Dynamic types and ASP.NET MVC
on 07-02-2011 11:16 PM

Dynamic types and asp net mvc.. Ho-o-o-o-t :)

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco <-- NEW Friend!
NServiceBus <-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)