Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html helper to show display name attribute without label

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.

like image 632
Pedre Avatar asked Dec 15 '22 21:12

Pedre


1 Answers

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);

}
like image 96
Raphaël Althaus Avatar answered Dec 26 '22 16:12

Raphaël Althaus