Created
September 9, 2011 01:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
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
lol