Typed View Data and Master Pages

An entry about asp.net mvc Publication date 7. August 2008 17:52

The ASP.NET MVC framework supports typed view data through the ViewPage<T> and ViewMasterPage<T> classes. Using these two classes together however, imposes a constraint caused by the generics involved; any ViewMasterPage<T> can only be used as the master page for pages that have the same type of view data. This means you cannot have a ViewMasterPage<T1> and then use that as the master for ViewPage<T2>, even if T2 inherits from T1.

For now, I’m using the following polymorphism trick to get around this:

public partial class Product : ViewPage<ApplicationInfo>
{
    ViewDataDictionary<ProductInfo> _viewData;
 
    public new ViewDataDictionary<ProductInfo> ViewData
    {
        get
        {
            if (null == _viewData) _viewData = base.ViewData.Convert<ApplicationInfo, ProductInfo>(); 
            return _viewData;
        }
    }
}

The Convert() extension method is defined like this:

public static class ViewDataDictionaryHelpers
{
    /// <summary>
    /// Converts the view data dictionary of <typeparamref name="T"/> to a view data dictionary of <typeparamref name="TNew"/>
    /// </summary>
    /// <typeparam name="T">The current type of the dictionary</typeparam>
    /// <typeparam name="TNew">The type of the dictionary to create</typeparam>
    /// <param name="viewData">The viewdata</param>
    /// <returns>The viewdata</returns>
    public static ViewDataDictionary<TNew> Convert<T, TNew>(this ViewDataDictionary<T> viewData) 
        where T : class 
        where TNew : class, T
    {
        ViewDataDictionary<TNew> newViewData = new ViewDataDictionary<TNew>((TNew)viewData.Model);
 
        foreach (KeyValuePair<string, object> value in viewData)
        {
            newViewData[value.Key] = value.Value;
        }
 
        return newViewData;
    }
}

It’s the best solution I’ve come up with so far, but I’m not perfectly happy with it. If you have any better ideas, leave a comment!

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.


Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS