Skip to content
20/07/2010 / Danresa Consultoria de Informática

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”

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:
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”
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”>
<SelectListItem> )Model.TimeZones) %>
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:

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

Public Function GetTimeZones() As ActionResult
Dim viewData As Object = New ExpandoObject()
viewData.TimeZones = From p In TimeZoneInfo.GetSystemTimeZones()


Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do

Você está comentando utilizando sua conta Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s

%d blogueiros gostam disto: