Beginning with version 2.0, ASP.NET MVC has shipped with a set of templates for both displaying and editing data.  These templates are buried within the System.Web.Mvc assembly.  While you can override them outright, you cannot easily extend them since they’re locked down.  The new MvcDisplayTemplates NuGet package fixes this problems.

[more]

If you’re not already familiar with ASP.NET MVC’s templating system, I strongly suggest you go check out Brad Wilson’s posts on the subject.  Though his posts are for MVC 2.0, they’re still just as relevant today as they were in 2009. 

The MvcDisplayTemplates package will add a DisplayTemplates folder to your project containing all of the standard templates:

image

They’ll behave very similarly to the built-in templates from the System.Web.Mvc assembly with one minor difference.  All the templates leverage a layout file that decorates the displayed value with an identifier generated from the model:

@using Microsoft.Web.Mvc
@model dynamic
<span id="@Html.IdForModel()">@RenderBody()</span>

This simple, benign change makes it far easier to discern where values from a view model are located within the output HTML.  Without that, frameworks such as SpecsFor.Mvc couldn’t easily locate values within the page.  If you don’t like this change, you can easily remove it by editing the _Layout.cshtml file.

If you’re wondering why I created this package, there are a couple of reasons.  First, I have recreated these templates numerous times now, and I’m a little tired of repeating myself.  Second, the default templates aren’t all that useful for SpecsFor.Mvc, and I needed a way to extend them easily.  So, MvcDisplayTemplates was born.  It is a very basic package, but I hope it provides a good starting point for anyone that wants to establish display conventions in their application.