Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joelpurra
Created April 16, 2012 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelpurra/2398934 to your computer and use it in GitHub Desktop.
Save joelpurra/2398934 to your computer and use it in GitHub Desktop.
MvcAnnotationHelper: Helps output formatted HTML from model properties.
//-----------------------------------------------------------------------
// <copyright file="MvcAnnotationHelper.cs" company="The Swedish Post and Telecom Authority (PTS)">
// Copyright (c) 2011, 2012 The Swedish Post and Telecom Authority (PTS)
// Developed for PTS by Joel Purra <http://joelpurra.se/>
// Released under the BSD license.
// </copyright>
//-----------------------------------------------------------------------
// https://gist.github.com/2398934
namespace JoelPurra.Web.Helpers
{
using System;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
/// <summary>
/// Helps output formatted HTML from model properties.
/// </summary>
public static class MvcAnnotationHelper
{
#region Constants and Fields
private const string DescriptionTagFormat = @"<span class=""description help-inline"">{0}</span>";
private const string NameTagFormat = @"<span class=""name"">{0}</span>";
private const string ShortNameTagFormat = @"<span class=""short-name"">{0}</span>";
#endregion
#region Public Methods
/// <summary>
/// Gets the data annotation metadata description for a property on the model.
/// </summary>
/// <typeparam name="TModel"> The annotated MVC model. </typeparam>
/// <typeparam name="TValue"> The model property value. </typeparam>
/// <param name="self"> The html output helper to use. </param>
/// <param name="expression"> Gets the property on the model to get the annotation from. </param>
/// <returns> A span tag containing the description. </returns>
public static MvcHtmlString AnnotatedDescriptionFor<TModel, TValue>(
this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression)
{
Contract.Requires(self != null);
Contract.Requires(expression != null);
return StringFromModel(self, expression, metadata => metadata.Description, DescriptionTagFormat);
}
/// <summary>
/// Gets the data annotation metadata name for a property on the model.
/// </summary>
/// <typeparam name="TModel"> The annotated MVC model. </typeparam>
/// <typeparam name="TValue"> The model property value. </typeparam>
/// <param name="self"> The HTML output helper to use. </param>
/// <param name="expression"> Gets the property on the model to get the annotation from. </param>
/// <returns> A span tag containing the name. </returns>
public static MvcHtmlString NameFor<TModel, TValue>(
this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression)
{
Contract.Requires(self != null);
Contract.Requires(expression != null);
return StringFromModel(self, expression, metadata => metadata.DisplayName, NameTagFormat);
}
public static MvcHtmlString ShortLabelFor<TModel, TValue>(
this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
Contract.Requires(html != null);
Contract.Requires(expression != null);
return html.LabelFor(expression, GetPropertyValue(html, expression, metadata => metadata.ShortDisplayName));
}
/// <summary>
/// Gets the data annotation metadata short name for a property on the model.
/// </summary>
/// <typeparam name="TModel"> The annotated MVC model. </typeparam>
/// <typeparam name="TValue"> The model property value. </typeparam>
/// <param name="self"> The HTML output helper to use. </param>
/// <param name="expression"> Gets the property on the model to get the annotation from. </param>
/// <returns> A span tag containing the short name. </returns>
public static MvcHtmlString ShortNameFor<TModel, TValue>(
this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression)
{
Contract.Requires(self != null);
Contract.Requires(expression != null);
return StringFromModel(self, expression, metadata => metadata.ShortDisplayName, ShortNameTagFormat);
}
#endregion
#region Methods
private static string GetPropertyValue<TModel, TValue>(
HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Func<ModelMetadata, string> property)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
string propertyValue = property(metadata);
return propertyValue;
}
/// <summary>
/// Extracts and formats a data annotation.
/// </summary>
/// <remarks>
/// From http://stackoverflow.com/questions/6578495/how-do-i-display-the-displayattribute-description-attribute-value
/// </remarks>
/// <typeparam name="TModel"> The annotated MVC model. </typeparam>
/// <typeparam name="TValue"> The model property value. </typeparam>
/// <param name="self"> The HTML output helper to use. </param>
/// <param name="expression"> Gets the property on the model to get the annotation from. </param>
/// <param name="property"> The data annotation metadata property to use. </param>
/// <param name="tagFormat"> Formatting string for the output where <code>{0}</code> will be replaced with the value of <paramref
/// name="property" /> . </param>
/// <returns> A user-formatted HTML-string. </returns>
private static MvcHtmlString StringFromModel<TModel, TValue>(
HtmlHelper<TModel> self,
Expression<Func<TModel, TValue>> expression,
Func<ModelMetadata, string> property,
string tagFormat)
{
Contract.Requires(self != null);
Contract.Requires(expression != null);
Contract.Requires(property != null);
Contract.Requires(tagFormat != null);
string propertyValue = GetPropertyValue(self, expression, property);
return MvcHtmlString.Create(string.Format(tagFormat, propertyValue));
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment