public class Board
{
[Display(ResourceType=(typeof(MVC.Resources.Board)), Name="TEST")]
[DisplayFormat(NullDisplayText="")]
public int? ItemId { get; set; }
public string Title { get; set; }
public string Contents { get; set; }
public string Author { get; set; }
public DateTime Date { get; set; }
}
这是 MVC 模型, 如何实现 DisplayFormat(NullDisplayText) 本地化
最佳答案
以下是我所做的工作。
我创建了以下类,可以为任何属性本地化 NullDisplayText。
public class LocalizedNullDisplayText : DisplayFormatAttribute
{
private readonly PropertyInfo _propertyInfo;
public LocalizedNullDisplayText(string resourceKey, Type resourceType)
: base()
{
_propertyInfo = resourceType.GetProperty(resourceKey, BindingFlags.Static | BindingFlags.Public);
if (_propertyInfo == null) return;
base.NullDisplayText = (string)_propertyInfo.GetValue(_propertyInfo.DeclaringType, null);
}
}
我是这样引用的:
[LocalizedNullDisplayText("ReviewerName_NullTextDisplay", typeof(RestaurantReviewResources))]
public string ReviewerName { get; set; }
它就像一个魅力。
关于c# - Model Class DisplayFormat() 如何本地化 NullDisplayText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25497036/