Using Dynamic Views In ASP.NET MVC 2

Posted by: Malcolm Sheridan , on 6/30/2010, in Category ASP.NET MVC
Views: 60389
Abstract: The following article demonstrates how to use dynamic views in ASP.NET MVC 2.
A really cool addition to ASP.NET MVC 2 is the ability to use the dynamic type as the object passed into the view. This is also known as a dynamic view. If you create a new view and don't create a strongly typed view, out of the box, the MVC framework will make the view inherit from the ViewPage class, but the generic type will be dynamic, like this:
<%@Page Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage"
<dynamic>"%>
 
Using the dynamic type is good because it allows you to create your objects on the fly. The downside of this is because the view is not strongly typed, you don't get the compile time checking. For me compile time checking is a compelling reason to use strongly typed views.
In this example, I'm going to pass a collection of time zones into the view as a dynamic object to the view. To see this in action, I'm going to create a small ASP.NET MVC 2 website. If you haven't got Microsoft Visual Studio 2010, you can download the Express edition here
To begin, let's add the view straight away. Normally you would do this after you have defined your action, but that's not the focus of this article. By adding a view that is not strongly, we get to use the dynamic type. Here's the view:
AddView
The HTML that's added inherits from the ViewPage class, but the generic type is dynamic:
<%@Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage"
<dynamic>"%>

Cool huh?! Now I can add a dynamic object. I'm going to be displaying the list of time zones on your computer in a drop down list. Here's the code:
<asp:ContentID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 <%:Html.DropDownList("DisplayName",(IEnumerable=""
<SelectListItem> )Model.TimeZones) %>
</asp:Content>
 
I can access the dynamic type via the Model property which is available to the view by default. If you compiled this now, even though I haven't created a real object, it will still compile. 
 
Ok let's add code to the controller to return the list of time zones. I'm adding this to the HomeController. Here's the code below:
 
C#
 
public ActionResult GetTimeZones()
{
       dynamic viewData = new ExpandoObject();
       viewData.TimeZones = from p in TimeZoneInfo.GetSystemTimeZones()
                                 select new SelectListItem
                                 {
                                    Text = p.DisplayName,
                                    Value = p.Id
                                 };
       return View("Index", viewData);

VB.NET

Public Function GetTimeZones() As ActionResult
         Dim viewData As Object = New ExpandoObject()
         viewData.TimeZones = From p In TimeZoneInfo.GetSystemTimeZones()
                              Select New SelectListItem With {.Text = p.DisplayName, .Value = p.Id}
         Return View("Index", viewData)
End Function

Notice the first line in the action? 
 
dynamic viewData = new ExpandoObject();
The ExpandoObject represents an object whose members can be dynamically added and removed at run time. Because of this, I can dynamically create a property called TimeZones, or anything else I'd like to define, and pass the dynamic object as the model to the view. The benefit of this is I don't need to define a separate class and pass that in as the model. The downside of this is if this isn't your code, debugging it could be hard as the objects could be hard to find.
 
This is another new feature of ASP.NET MVC 2. I'll be addressing new features in the framework in upcoming posts. The entire source code of this article can be downloaded over here
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by David on Wednesday, June 30, 2010 5:42 PM
Presumably you're talking about ASP.NET MVC 2 for .NET 4.0?
Comment posted by Martin VU on Wednesday, June 30, 2010 11:06 PM
I was trying to get my head around dynamic views and finally get it now! thanks
Comment posted by Malcolm Sheridan on Thursday, July 1, 2010 7:59 AM
@David
Yes.  Even though MVC 2 runs on ASP.NET 3.5, dynamic views only run on ASP.NET 4.

@Martin
Glad you enjoyed it.
Comment posted by Pola on Tuesday, July 6, 2010 11:20 PM
I was looking for exactly something similar last week - excellent!
Comment posted by Daniel on Wednesday, September 15, 2010 5:01 AM
I used DropDownlist as mentioned above. It works fine . But if i use a textbox following error occurs
'System.Web.Mvc.HtmlHelper<dynamic>' has no applicable method named 'TextBox' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

Comment posted by James on Friday, June 10, 2011 5:33 AM
@Daniel I get the same issue. Html.Editor works, but Html.TextBox doesn't.
Comment posted by buy Francesco reps on Sunday, June 19, 2011 10:24 PM
The benefit of this is I don't need to define a separate class and pass that in as the model. http://www.buyreps.com/buy-francesco-reps-1812.html The downside of this is if this isn't your code, debugging it could be hard as the objects could be hard to find.buy Francesco reps

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel