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

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

问题描述

在我的MVC5应用程序中,我有一个枚举类,如下所示,使用这种方法,我可以将枚举值(即美国,英国,而不是美国)从控制器传递给视图,我如何传递和显示枚举描述方法?我尝试过许多不同的解决方法,如 C#String枚举等,但没有一个解决我的问题,另一方面,我不想使用密封类,对于我来说,更好的解决方案是枚举类,如下所示:






枚举:

  public枚举国家
{
[描述(美国)]
US = 1,
[描述(英国)]
UK = 2,
[描述(新西兰)]
新西兰= 3,
[描述(法国)]
法国= 4,
[说明(德国 )]
德国= 5
}



型号:

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

public国家国家{get;组; }
//为简洁起见省略代码
}



控制器:

  public JsonResult Visitor_Read([DataSourceRequest] DataSourceRequest请求)
{
var result = db.Visitors.Select(m => new VisitorViewModel
{
VisitorID = m.VisitorID,
Country = m.Country
//代码省略为简洁
})
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
}
},
模式:{
模型:{
fields: {
VisitorID:{type:'number'},
国家:{类型:'string'}
}
}
}
},
列:
[
{field:VisitorID,title:Id},
{field:Country,title:Country},
]
})。data(kendoGrid);

});


解决方案

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

 使用System.ComponentModel.DataAnnotations.Schema; 
public class VisitorViewModel
{
[Key]
public int VisitorID {get;组; }

public国家国家{get;组; }

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

GetDescription() 是下一个扩展方法:

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

返回属性!= null? ((DescriptionAttribute)属性).Description:field;
}


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

});

解决方案

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

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

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