Cannot display enum values on Kendo grid

后端 未结 2 1710
悲&欢浪女
悲&欢浪女 2020-12-10 23:23

In my MVC5 application I have an enum class as shown below and with this approach I can pass the enum values i.e. US, UK instead of United States\" from Controller to View.

相关标签:
2条回答
  • 2020-12-10 23:30

    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;
    }
    
    0 讨论(0)
  • 2020-12-10 23:47

    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" }, 
    
    0 讨论(0)
提交回复
热议问题