无法在Kendo网格上显示枚举值 [英] Cannot display enum values on Kendo grid
问题描述
枚举:
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屋!