Cannot display enum values on Kendo grid

2019-01-19 09:48发布

问题:

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. How can I pass and display enum description with the following approach? I tried many different solution method as C# String enums, etc. but none of them solved my problem. On the other hand, I do not want to use sealed class and it would be better for me a solution with enum class as shown below:


Enum:

public enum Country
{
    [Description("United States")]
    US = 1,
    [Description("United Kingdom")]
    UK = 2,
    [Description("New Zealand")]
    NewZealand = 3,
    [Description("France")]
    France = 4,
    [Description("Germany")]
    Germany = 5
}


Model:

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

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


Controller:

public JsonResult Visitor_Read([DataSourceRequest] DataSourceRequest request)
{
    var result = db.Visitors.Select(m => new VisitorViewModel
    {
        VisitorID = m.VisitorID,
        Country = m.Country
        //code omitted for brevity
    })      
    var jsonResult = Json(result, JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;
    return jsonResult;
}


View:

$(document).ready(function () {

    var grid = $("#visitorGrid").kendoGrid({            
        dataSource: {
            type: "json",
            transport: {
                read: {
                    url: "/Visitor/Visitor_Read",
                    dataType: "json",
                    cache: false
                }
            },
            schema: {
                model: {
                    fields: {
                        VisitorID: { type: 'number' },
                        Country : { type: 'string' }
                    }
                }
            }
        },
        columns:
        [   
            { field: "VisitorID", title: "Id" },
            { field: "Country ", title: "Country" }, 
        ]
    }).data("kendoGrid");   

});

回答1:

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


回答2:

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" },