Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the ShortName property of the Display attribute for my table headers>

I see the DisplayAttribute has a ShortName property, but I see no Html.ShortName helper. How can I get to use this short name for my table column headings? Do I have to write my own helper?

like image 970
ProfK Avatar asked Dec 21 '22 11:12

ProfK


1 Answers

You could write your own helper :

Something like

public static IHtmlString ShortLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression) {
   var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
   var content = metadata.ShortDisplayName?? metadata.DisplayName ?? /*something else*/ ?? string.Empty;
   return new HtmlString(content);
}

But, from msdn :

The short display name can be used in a tooltip or in other display contexts such as the title of tabular list views where the complete display name might not fit. For example, in MVC, this name is used in tables where the columns are not wide enough to display the complete field name. If this field is null, DisplayName should be used.

So it looks like it should be automatic (when no room enough), but... untested here. Sounds like it should work with @Html.LabelFor in this way.

like image 84
Raphaël Althaus Avatar answered Mar 01 '23 22:03

Raphaël Althaus