我如何使用显示属性的短名称属性我的表头> [英] How can I use the ShortName property of the Display attribute for my table headers>

查看:125
本文介绍了我如何使用显示属性的短名称属性我的表头>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到 DisplayAttribute 有一个 SHORTNAME 属性,但我没有看到 HTML。 SHORTNAME 帮手。我怎样才能为我的表的列标题使用此短的名字吗?我必须写我自己的助手?

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?

推荐答案

您可以编写自己的助手:

You could write your own helper :

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

不过,从<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.modelmetadata.shortdisplayname%28v=vs.100%29.aspx\">msdn

简短显示名称可以在工具提示中或在其他显示器中使用
  上下文如表格列表视图标题,其中完整
  显示名称可能不适合。例如,在MVC,这个名字中使用
  表,其中列的宽度不够显示完整的
  字段名。如果该字段为空,显示名称应使用。

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.

所以它看起来像它应该是自动的(当没有足够的空间),但未经检验......在这里。听起来像它应该与@ Html.LabelFor工作,以这种方式。

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.

这篇关于我如何使用显示属性的短名称属性我的表头&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆