Skip to content

Instantly share code, notes, and snippets.

@ryankirkman
Created September 9, 2011 01:59
Show Gist options
  • Save ryankirkman/1205315 to your computer and use it in GitHub Desktop.
Save ryankirkman/1205315 to your computer and use it in GitHub Desktop.
Html Helper to return the raw DisplayName attribute of a property of a Model
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
namespace MvcHtmlHelpers
{
public static class HtmlHelperExtensions
{
/// <summary>
/// Return the raw DisplayName attribute of a property of a Model,
/// a-la @Html.LabelFor, without the label.
/// </summary>
/// <typeparam name="TModel">Model to access</typeparam>
/// <typeparam name="TValue">Property of model to access.</typeparam>
/// <param name="self">Default parameter. Always passed.</param>
/// <param name="expression">Lambda expression to evaluate.</param>
/// <returns>DisplayName as a raw String with no markup.</returns>
public static MvcHtmlString DisplayNameFor<TModel, TValue>(
this HtmlHelper<TModel> self,
Expression<Func<TModel, TValue>> expression
)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
return new MvcHtmlString(metadata.DataTypeName);
}
}
}
@thomasdavis
Copy link

lol

@ryankirkman
Copy link
Author

We don't love them .NET method signatures, and it's like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment