c# - Model Class DisplayFormat() 如何本地化 NullDisplayText?

标签 c# asp.net-mvc localization displayformat

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/

相关文章:

c# - 序列化类型化数据集、更新程序集、反序列化错误

asp.net-mvc - mvc [DataType(DataType.EmailAddress)无需验证

asp.net - 如何在 ASP.NET MVC5 Identity 2 中添加用于电子邮件确认的 html 电子邮件模板?

go - 如何在 golang 中获得实际的本地化/语言?

android - 使用 resConfig 为 Android 风格强制语言环境

macos - Swift 字符串变量本地化

c# - 如何从 DataSet 中的 DataTable 存储中检索数据并将其存储在数组中?

javascript - 我可以从 Knockout Foreach 模板获取索引并传递给 Html.EditorFor 吗?

c# - XML 中命名空间的区别

c# - 试图了解如何将变量传递给自定义验证?