Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

asp.net mvc - Cannot display enum values on Kendo grid

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

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...