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 :)

bookmarking submission wrote re: Dynamic types and ASP.NET MVC
on 01-18-2013 1:32 PM

kf16tT I truly appreciate this blog article.Really looking forward to read more. Cool.

pills for lose weight wrote re: Dynamic types and ASP.NET MVC
on 02-01-2013 9:50 AM

bODAPl Very informative post. Cool.

buy viagra online wrote re: Dynamic types and ASP.NET MVC
on 02-02-2013 6:20 PM

KONYz1 This is one awesome blog.

buy imitrex generic wrote re: Dynamic types and ASP.NET MVC
on 02-15-2013 1:50 PM

XSeNEy Awesome blog.Thanks Again. Keep writing.

buy stendra generic wrote re: Dynamic types and ASP.NET MVC
on 02-24-2013 4:38 PM

DsodI5 Very good blog post.Much thanks again. Really Great.

buy clomid no prescription wrote re: Dynamic types and ASP.NET MVC
on 02-28-2013 8:52 AM

PL7V7h Im grateful for the article post.Really thank you!

buy clomid no prescription wrote re: Dynamic types and ASP.NET MVC
on 02-28-2013 4:07 PM

c2Gknv wow, awesome blog post.Really thank you!

buy discount viagra online wrote re: Dynamic types and ASP.NET MVC
on 03-03-2013 10:16 PM

qJhNQz I value the post.Really thank you! Really Great.

click here wrote re: Dynamic types and ASP.NET MVC
on 03-03-2013 10:41 PM

l4Hhpk A round of applause for your article.Thanks Again. Fantastic.

bookmarks wrote re: Dynamic types and ASP.NET MVC
on 03-13-2013 6:10 PM

Y2druR Hey, thanks for the article post.Really thank you! Much obliged.

Social bookmarks wrote re: Dynamic types and ASP.NET MVC
on 03-23-2013 5:26 AM

3il9F6 Great, thanks for sharing this blog article.Really thank you! Really Cool.

comedy channel wrote re: Dynamic types and ASP.NET MVC
on 04-06-2013 9:29 AM

Thanks a lot for the blog post.Much thanks again. Really Cool.

Social bookmarks wrote re: Dynamic types and ASP.NET MVC
on 04-19-2013 7:25 AM

b9dSSZ Im grateful for the blog post.Really thank you! Keep writing.

Social bookmarks wrote re: Dynamic types and ASP.NET MVC
on 04-24-2013 3:02 AM

XTYnqB Really informative article. Great.

camera buying guide wrote re: Dynamic types and ASP.NET MVC
on 05-14-2013 3:49 AM

b7T1sf I really enjoy the blog article.Thanks Again. Will read on...

social bookmarking service wrote re: Dynamic types and ASP.NET MVC
on 06-05-2013 7:50 PM

dlmnue I am so grateful for your blog post.Really thank you! Awesome.

best social bookmarks wrote re: Dynamic types and ASP.NET MVC
on 06-19-2013 5:35 PM

mw4iDQ Thanks for sharing, this is a fantastic post.Really looking forward to read more.

cheap bookmarks wrote re: Dynamic types and ASP.NET MVC
on 06-21-2013 3:03 AM

QcHAKB Enjoyed every bit of your blog post. Great.

news wrote re: Dynamic types and ASP.NET MVC
on 07-09-2013 12:08 AM

im8bej Really enjoyed this blog. Keep writing.

news wrote re: Dynamic types and ASP.NET MVC
on 07-11-2013 9:28 PM

RxMiZ4 Very good blog article.Really thank you! Awesome.

buy viagra online wrote re: Dynamic types and ASP.NET MVC
on 07-24-2013 9:33 AM

Say, you got a nice post.Much thanks again. Want more.

buy cialis online cheap wrote re: Dynamic types and ASP.NET MVC
on 07-25-2013 11:51 AM

Hey, thanks for the blog.Thanks Again. Really Cool.

news wrote re: Dynamic types and ASP.NET MVC
on 07-26-2013 9:15 AM

PONzkr Really appreciate you sharing this blog post.Really looking forward to read more. Really Cool.

cheap social bookmarks wrote re: Dynamic types and ASP.NET MVC
on 07-28-2013 5:40 PM

ACXqK6 Thanks so much for the blog.Thanks Again. Awesome.

best news wrote re: Dynamic types and ASP.NET MVC
on 08-02-2013 11:09 AM

Fantastic blog article.Really looking forward to read more.

best news wrote re: Dynamic types and ASP.NET MVC
on 08-02-2013 4:00 PM

inJsv5 Thanks-a-mundo for the blog post.Really thank you! Fantastic.

awesome moldavian news wrote re: Dynamic types and ASP.NET MVC
on 08-05-2013 12:08 AM

eEcGx1 Enjoyed every bit of your blog.Thanks Again. Want more.

awesome links for you wrote re: Dynamic types and ASP.NET MVC
on 08-19-2013 11:06 AM

FXbL3B I appreciate you sharing this article.Much thanks again. Fantastic.

awesome links for you wrote re: Dynamic types and ASP.NET MVC
on 08-19-2013 10:40 PM

SLPGFp Very good article.Really thank you! Cool.

great link buildng wrote re: Dynamic types and ASP.NET MVC
on 08-22-2013 4:16 PM

VHB62q I truly appreciate this article post.Really looking forward to read more. Keep writing.

good seo guys wrote re: Dynamic types and ASP.NET MVC
on 09-03-2013 11:56 PM

zC6tUP Great post.Really looking forward to read more. Cool.

seo service wrote re: Dynamic types and ASP.NET MVC
on 09-05-2013 9:29 AM

sj6zGt Thanks so much for the blog post.Really thank you! Much obliged.

best place for make money online wrote re: Dynamic types and ASP.NET MVC
on 09-11-2013 6:43 PM

ZxnPi3 Great post.Much thanks again. Really Cool.

only for 5 dollars wrote re: Dynamic types and ASP.NET MVC
on 09-13-2013 5:49 AM

HbaLrI This is one awesome post.Really thank you! Really Cool.

pro link building wrote re: Dynamic types and ASP.NET MVC
on 09-24-2013 12:36 PM

DuCezr A round of applause for your blog article.Thanks Again. Will read on...

check out seo wrote re: Dynamic types and ASP.NET MVC
on 09-29-2013 12:34 PM

2No4KA Very neat blog. Awesome.

awesome linkbuilding site wrote re: Dynamic types and ASP.NET MVC
on 10-01-2013 6:19 AM

hoSXmu A big thank you for your blog post.Really thank you!

link building team wrote re: Dynamic types and ASP.NET MVC
on 10-16-2013 2:19 AM

6qLVSv Im thankful for the article.Much thanks again. Awesome.

take a look at it! wrote re: Dynamic types and ASP.NET MVC
on 10-25-2013 3:18 AM

YHlvF5 Looking forward to reading more. Great article.Really thank you! Will read on...

smashing top seo wrote re: Dynamic types and ASP.NET MVC
on 11-01-2013 6:09 AM

XaWZW9 Thanks-a-mundo for the blog.Really thank you!

crorkservice wrote re: Dynamic types and ASP.NET MVC
on 07-18-2014 3:34 PM

w7IsVP Looking forward to reading more. Great blog.Really thank you! Cool.

matzcrorkz wrote re: Dynamic types and ASP.NET MVC
on 08-06-2014 12:48 PM

7JkNHK Muchos Gracias for your blog.Really looking forward to read more. Cool.

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
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)