Framework Madness!

And other adventures C# and …

Posts Tagged ‘C# MVC 2(preview 1) and Anonymous Delegates in Views

leave a comment »


I have been studying MVC for a few months and with the release of MVC 2 preview 1 I have decided to jump in.

But first I wanted to check on something. With MVC using the Web Forms view engine you get a return to spaghetti code, along with a few helper classes. But to be honest, if I can avoid writing out decision simple logic structures in views such as “if/then’ I would do so. Now is classic asp this is for the most part impossible, but I think I may have stumbled on how to do this with anonymous delegates.

To use an anonymous delegate first you have to have a delegate. In this case, the delegate I have is for a model, so I’ll show you both:

   1: public delegate void TestTemplate<T>(T TemplateModel);


   3: public class TestModel

   4: {


   6:     public String Name { get; set; }


   8:     public string Test { get; set; }

   9: }

Nothing really special. The delegate is a generic and can take any type to pass in an model parameter. And our model is really simple, just a simple test object with two properties.

Now let’s get to the heart of the matter. The first thing I did was just to add a method to the Home controller.

   1: public  ActionResult Test()

   2: {

   3:    var _Test = new TestModel() {Name = "Name Value", Test = "Test Value"};



   6:    return View(_Test);

   7: }

This simple methods just makes a TestModel instance and returns a Strong Type view, passing the test object. Here’s the view:

Okay. The anonymous delegate signature is simple ‘TestTemplate<TestModel> holder = delegate(TestModel Model)’. This will create a ‘holder’ object that can be passed around just like any other object. The important thing is just like with normal C# you cannot use an anonymous delegate until it has been declared. After the delegate signature comes the body that contains a mix of html and and other markup. I’ve added a comment for were the delegate body ends.

   1: <%@ Page Title="" Language="C#" 

   2:     MasterPageFile="~/Views/Shared/Site.Master" 

   3:     Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.TestModel>" %>

   4: <%@ Import Namespace="MvcApplication1.Models" %>

   5: <%@ Import Namespace="MvcApplication1" %>

   6: <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

   7:     Test

   8: </asp:Content>

   9: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

  10:     <h2>

  11:         Test</h2>

  12:     <% TestTemplate<TestModel> holder = delegate(TestModel Model)

  13:        {%>

  14:     <p>

  15:         <% =Html.LabelFor(m=> m.Name)%>

  16:         <% =Html.DisplayFor(m=>m.Name) %></p>

  17:     <p>

  18:         <% =Html.LabelFor(m=> m.Test) %>

  19:         <% =Html.DisplayFor(m=>m.Test) %>

  20:     </p>

  21:     <%}; %>

  22:     <%

  23:         if (this.Model != null)

  24:         {

  25:             TestTemplateBinder.Bind<TestModel>(holder, this.Model);

  26:         }

  27:     %>


  29: </asp:Content>

Lastly, a simple bit of view logic. Now just for testing purposes I have created a binder class (see code below) that has a static method that actually invokes the delegate, but I could have just written ‘holder.Invoke(this.Model)’ and that would have worked. But I wanted to demonstrate that just like any C# object this code can be passed around.  Here is the code for TestTemplateBinder:

   1: public static class TestTemplateBinder

   2:     {

   3:         public static void Bind<T>(TestTemplate<T> Template, T Model)

   4:         {

   5:             if (Template != null && Model != null)

   6:             {

   7:                 Template.Invoke(Model);

   8:             }

   9:         }


  11:     }

So I am thinking this could be used in templating  scenarios where you may want to encapsulate the decision making logic in a helper but to want to keep the code mark on the page.


Written by Lynn Eriksen

August 16, 2009 at 6:34 am

Posted in Uncategorized

Tagged with ,