|
Using Dynamic Views In ASP.NET MVC 2
|
|
Rating: 11 user(s) have rated this article
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:
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.