我有这个:
[Display(Name = "Empresa")]
public string Company{ get; set; }
在我的 aspx 中我有:
<th><%: Html.LabelFor(model => model.Company)%></th>
这会产生:
<th><label for="Company">Empresa</label></th>
是否有任何 html 辅助扩展只显示没有标签的显示属性,只有纯文本?我想要的输出是这样的:
<th>Empresa</th>
谢谢!
编辑
我按照建议尝试了 DisplayFor 或 DisplayTextFor,但它们无效,因为它们生成:
<th>Amazon</th>
它们返回属性的值...我想要 Display 属性中的名称。
最佳答案
后期编辑
在 >= MVC4 中,只需使用 @Html.DisplayNameFor
在那之前
使用你自己的助手
public static MvcHtmlString SimpleLabelFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression
) {
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string resolvedLabelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(resolvedLabelText))
return MvcHtmlString.Empty;
return MvcHtmlString.Create(resolvedLabelText);
}
关于c# - 显示不带标签的显示名称属性的 Html 帮助程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11447801/