Model Class DisplayFormat() How can I localization NullDisplayText?

1.4k Views Asked by At
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; }
}

this it MVC Model, How Can I DisplayFormat(NullDisplayText) localization

2

There are 2 best solutions below

3
On BEST ANSWER

Here's what I did that works.

I created the following class that could localize the NullDisplayText for any property.

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

I referenced it like this:

[LocalizedNullDisplayText("ReviewerName_NullTextDisplay", typeof(RestaurantReviewResources))]
public string ReviewerName { get; set; }

It works like a charm.

0
On
    public class LocalizedDisplayFormatAttribute : DisplayFormatAttribute
{
    public LocalizedDisplayFormatAttribute()
        : base()
    {
    }

    public new string NullDisplayText
    {
        get
        {
            return base.NullDisplayText;
        }
        set
        {
            base.NullDisplayText = /* Return Text */;
        }
    }
}