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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s