I’d like to have the functionality of printing out a model’s member name/display name. This is akin to using HtmlHelper<TModel>.LabelFor but having the text not wrapped in a label. Is there an extension method already built into MVC for this exact purpose? I couldn’t find one so I looked at what LabelFor does with Reflector and made a LabelTextFor method:
public static string LabelTextFor<TModel, TValue>(this HtmlHelper<TModel> source, Expression<Func<TModel, TValue>> expression)
{
var memberName = ExpressionHelper.GetExpressionText((LambdaExpression)expression);
var metadata = ModelMetadata.FromLambdaExpression(expression, new ViewDataDictionary<TModel>());
return metadata.DisplayName ?? metadata.PropertyName ?? memberName.Split('.').Last();
}
I’m thinking I should maybe change the name as well of this method. Thoughts are appreciated.
I’ve decided to use the method that I’ve created.