Cannot display enum values on Kendo grid

本秂侑毒 提交于 2019-11-28 11:50:19

You must set NotMapped attribute for custom property:

using System.ComponentModel.DataAnnotations.Schema;
public class VisitorViewModel
{
    [Key]
    public int VisitorID { get; set; }

    public Country Country { get; set; }

    [NotMapped]
    public string CountryName
    {
        get { return Country.GetDescription(); }
    }
}

and GetDescription() is next extension method:

public static string GetDescription(this Enum e)
{
    var field = e.ToString();
    var attribute = e.GetType().GetField(field).GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();

    return attribute != null ? ((DescriptionAttribute)attribute).Description : field;
}

You will have to create method which will return description attribute. It can be some helper method, extension or whatever you want.

For example:

public class VisitorViewModel
{
    [Key]
    public int VisitorID { get; set; }

    public Country Country { get ; set; }
    //code omitted for brevity

    public  string GetDescription()
    {
        var type = typeof(Country);
        var memInfo = type.GetMember(this.Country.ToString());
        var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
        return ((DescriptionAttribute)attributes[0]).Description;
    }
}

so you can call it like

var result = db.Visitors.Select(m => new VisitorViewModel
{
    VisitorID = m.VisitorID,
    Country = m.GetDescription()
    //code omitted for brevity
})  

Or if it is better for you, create helper method which will be called similarly, but will be static ...

public class SomeHelperClass
{
    public static string GetDescription(VisitorViewModel model)
    {
        var type = typeof(Country);
        var memInfo = type.GetMember(model.Country.ToString());
        var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
        return ((DescriptionAttribute)attributes[0]).Description;
    }
}

so call will be looks like

SomeHelperClass.GetDescription(model);

EDIT I got one idea, maybe it is not exactly what you want, maybe it can help you. If you add property with country name you can use this approach also:

public class VisitorViewModel
{
    [Key]
    public int VisitorID { get; set; }

    public string CountryName { get; set; }

    private Country _country;
    public Country Country 
    { 
        get { return this._country; }
        set
        {
            this._country = value;
            this.CountryName = GetDescription(value);
        }
    }
    //code omitted for brevity

    private string GetDescription(Country country)
    {
        var type = typeof(Country);
        var memInfo = type.GetMember(country.ToString());
        var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
        return ((DescriptionAttribute)attributes[0]).Description;
    }
}

so if you will fill your model as you do

var result = db.Visitors.Select(m => new VisitorViewModel
{
    VisitorID = m.VisitorID,
    Country = m.Country
    //code omitted for brevity
})    

you will have automatically filled your CountryName property whcih can be use in kendo grid.

{ field: "CountryName", title: "Country" }, 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!