Create new account I forgot my password    

Using Dynamic Views In ASP.NET MVC 2
Rating: 11 user(s) have rated this article Average rating: 4.5
Posted by: Malcolm Sheridan, on 6/30/2010, in category "ASP.NET MVC"
Views: this article has been read 16079 times
Abstract: The following article demonstrates how to use dynamic views in ASP.NET MVC 2.

Using 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

If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email

Malcolm Sheridan is an ASP.NET MVP and an independent contractor who has been working with Microsoft technologies since VB4. Malcolm has worked with .NET since its inception and thoroughly enjoys ASP.NET.
 









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

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 01, 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 06, 2010 11:20 PM
I was looking for exactly something similar last week - excellent!

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

NEWSLETTER