Using Dynamic Views In ASP.NET MVC 2
Posted by: Malcolm Sheridan ,
on 6/30/2010,
in
Category ASP.NET MVC
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:
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
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
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