I would like to have the print function name / display name of a model member. This is similar to use HtmlHelper<TModel>.LabelFor, but with text not wrapped in label. Is there an extension method already built into MVC for this purpose? I could not find it, so I looked at what LabelForReflector was doing and made a method LabelTextFor:
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 think I should change the name of this method. Thoughts are evaluated.
source
share