Ped*_*dre 4 html c# asp.net-mvc html-helper
我有这个:
[Display(Name = "Empresa")]
public string Company{ get; set; }
Run Code Online (Sandbox Code Playgroud)
在我的aspx中我有:
<th><%: Html.LabelFor(model => model.Company)%></th>
Run Code Online (Sandbox Code Playgroud)
这会产生:
<th><label for="Company">Empresa</label></th>
Run Code Online (Sandbox Code Playgroud)
是否有任何html帮助扩展只显示没有标签的display属性,只显示纯文本?我想要的输出是这样的:
<th>Empresa</th>
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑
我按照建议尝试了DisplayFor或DisplayTextFor,但它们无效,因为它们生成:
<th>Amazon</th>
Run Code Online (Sandbox Code Playgroud)
它们返回属性的值...我想要Display属性中的名称.
Rap*_*aus 12
晚编辑
在> = 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);
}
Run Code Online (Sandbox Code Playgroud)