<p>
<%= Html.Encode(
ModelMetadata.FromLambdaExpression<YourViewModel, string>(
x => x.SomeProperty, ViewData).DisplayName
) %>
<p>
Obviously in order to avoid the spaghetti code it is always a good idea to write a helper:
public static class HtmlExtensions
{
public static MvcHtmlString GetDisplayName<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression
)
{
var metaData = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
string value = metaData.DisplayName ?? (metaData.PropertyName ?? ExpressionHelper.GetExpressionText(expression));
return MvcHtmlString.Create(value);
}
}
And then:
<p>
<%: Html.GetDisplayName(x => x.SomeProperty) %>
</p>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…