Display属性中的ShortName(DataAnnotations) [英] ShortName in the Display attribute (DataAnnotations)

查看:103
本文介绍了Display属性中的ShortName(DataAnnotations)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Name属性可以正常工作,但ShortName不起作用.

[Display(Name = "Date of the transfer the task", ShortName = "Trans date")]
public DateTime TransferDate { get; set; }

即使我删除Name属性,ShortName也将被忽略(列标题中显示"TransferDate").

在视图中,我这样做:

@Html.DisplayNameFor(model => model.TransferDate)

解决方案

如果您查看 ShortName属性. dataannotations.displayattribute.aspx"rel =" nofollow noreferrer"title =" System.ComponentModel.DataAnnotations.DisplayAttribute> Display属性,您会发现它的作用域非常有限:

当然,这并不限制您利用模型元数据上的该值,但是没有任何本机帮助程序可以这样做.

从MVC开始2 @ModelMetadata.FromLambdaExpression<RegisterModel, string>( model => model.TransferDate, ViewData).ShortDisplayName} )

但是添加扩展方法以确保访问的一致性,重复数据删除和改进的错误处理也是完全有效的

 public static class MvcHtmlHelpers
{
   public static MvcHtmlString ShortNameFor<TModel, TValue>(this HtmlHelper<TModel> self, 
           Expression<Func<TModel, TValue>> expression)
   {
       var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
       var name = metadata.ShortDisplayName ?? metadata.DisplayName ?? metadata.PropertyName;

       return MvcHtmlString.Create(string.Format(@"<span>{0}</span>", name));
   }
}
 

然后像使用其他任何辅助方法一样使用

:

 @Html.ShortNameFor(model => model.TransferDate)
 

进一步阅读:

Name attribute works proper, but ShortName doesn't work.

[Display(Name = "Date of the transfer the task", ShortName = "Trans date")]
public DateTime TransferDate { get; set; }

Even when I delete Name attribute, ShortName is ignored ("TransferDate" displays in the column header).

In the view I do this:

@Html.DisplayNameFor(model => model.TransferDate)

解决方案

If you look at the Description for the ShortName property on the Display Attribute you'll see that it has a pretty limited scope out of the box:

Of course, that doesn't limit you from leveraging that value on your Model Metadata, but there aren't any native helpers that do so.

Starting with MVC 2, ModelMetadata provides two methods to access the underlying data: FromStringExpression and FromLambdaExpression, so you don't really need to start from scratch in writing your own helper or extension method.

If you hate writing HTML helper methods, you can do this all inline:

@ModelMetadata.FromLambdaExpression<RegisterModel, string>( 
            model => model.TransferDate, ViewData).ShortDisplayName} )  

But it's also perfectly valid to add an extension method for consistency of access, deduplication of code, and improved error handling

public static class MvcHtmlHelpers
{
   public static MvcHtmlString ShortNameFor<TModel, TValue>(this HtmlHelper<TModel> self, 
           Expression<Func<TModel, TValue>> expression)
   {
       var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
       var name = metadata.ShortDisplayName ?? metadata.DisplayName ?? metadata.PropertyName;

       return MvcHtmlString.Create(string.Format(@"<span>{0}</span>", name));
   }
}

And then use like any other helper method:

@Html.ShortNameFor(model => model.TransferDate)

Further Reading:

这篇关于Display属性中的ShortName(DataAnnotations)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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