Reputation: 446
I have this:
[Display(Name = "Empresa")]
public string Company{ get; set; }
In my aspx I have:
<th><%: Html.LabelFor(model => model.Company)%></th>
And this generates:
<th><label for="Company">Empresa</label></th>
Are there any html helper extensions to only show the display attribute without label, only plain text? My desired output is this:
<th>Empresa</th>
Thanks!
EDIT
I tried DisplayFor or DisplayTextFor as suggested, but they are not valid because they generate:
<th>Amazon</th>
They return the value of the property... I want the name from the Display attribute.
Upvotes: 4
Views: 7416
Reputation: 60493
Late Edit
In >= MVC4, just use @Html.DisplayNameFor
Before that
use your own Helper
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);
}
Upvotes: 12