Model Class DisplayFormat () How can I localize the NullDisplayText?

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 is the MVC model, How can I display ( NullDisplayText ) localization

+3


source to share


2 answers


Here's what I did that works.

I created the following class that could localize 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.

+4


source


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

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

      



0


source







All Articles