无法在 Kendo 网格上显示枚举值 [英] Cannot display enum values on Kendo grid

查看:20
本文介绍了无法在 Kendo 网格上显示枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 MVC5 应用程序中,我有一个枚举类,如下所示,通过这种方法,我可以将枚举值(即 US、UK 而不是美国)从 Controller 传递到 View.如何通过以下内容传递和显示枚举描述方法?我尝试了许多不同的解决方法,如 C# String enums 等,但都没有解决我的问题.另一方面,我不想使用密封类,对我来说使用枚举类的解决方案会更好,如下所示:

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:


枚举:

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
}


型号:

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

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


控制器:

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


查看:

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

});

推荐答案

您必须为自定义属性设置 NotMapped 属性:

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

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

这篇关于无法在 Kendo 网格上显示枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆