无法在 Kendo 网格上显示枚举值
Cannot display enum values on Kendo grid
在我的 MVC5 应用程序中,我有一个枚举 class,如下所示,通过这种方法,我可以将枚举值(即美国、英国而不是美国)从控制器传递到视图。我如何传递和使用以下方法显示枚举描述?我尝试了许多不同的解决方法,如 C# String enums 等,但其中 none 解决了我的问题。另一方面,我不想使用密封 class 对我来说,使用枚举 class 的解决方案会更好,如下所示:
枚举:
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");
});
您必须创建 return 描述属性的方法。它可以是一些辅助方法、扩展或任何你想要的。
例如:
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;
}
}
所以你可以这样称呼它
var result = db.Visitors.Select(m => new VisitorViewModel
{
VisitorID = m.VisitorID,
Country = m.GetDescription()
//code omitted for brevity
})
或者,如果这对你更好,创建将以类似方式调用的辅助方法,但将是静态的...
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;
}
}
所以调用看起来像
SomeHelperClass.GetDescription(model);
编辑
我有一个想法,也许这不是你想要的,也许它可以帮助你。
如果您添加带有国家/地区名称的 属性,您也可以使用此方法:
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;
}
}
所以如果你像你一样填充你的模型
var result = db.Visitors.Select(m => new VisitorViewModel
{
VisitorID = m.VisitorID,
Country = m.Country
//code omitted for brevity
})
您将自动填写您的国家/地区名称 属性,可以在 kendo 网格中使用。
{ field: "CountryName", title: "Country" },
您必须为自定义 属性 设置 NotMapped
属性:
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()
是下一个扩展方法:
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;
}
在我的 MVC5 应用程序中,我有一个枚举 class,如下所示,通过这种方法,我可以将枚举值(即美国、英国而不是美国)从控制器传递到视图。我如何传递和使用以下方法显示枚举描述?我尝试了许多不同的解决方法,如 C# String enums 等,但其中 none 解决了我的问题。另一方面,我不想使用密封 class 对我来说,使用枚举 class 的解决方案会更好,如下所示:
枚举:
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");
});
您必须创建 return 描述属性的方法。它可以是一些辅助方法、扩展或任何你想要的。
例如:
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;
}
}
所以你可以这样称呼它
var result = db.Visitors.Select(m => new VisitorViewModel
{
VisitorID = m.VisitorID,
Country = m.GetDescription()
//code omitted for brevity
})
或者,如果这对你更好,创建将以类似方式调用的辅助方法,但将是静态的...
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;
}
}
所以调用看起来像
SomeHelperClass.GetDescription(model);
编辑 我有一个想法,也许这不是你想要的,也许它可以帮助你。 如果您添加带有国家/地区名称的 属性,您也可以使用此方法:
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;
}
}
所以如果你像你一样填充你的模型
var result = db.Visitors.Select(m => new VisitorViewModel
{
VisitorID = m.VisitorID,
Country = m.Country
//code omitted for brevity
})
您将自动填写您的国家/地区名称 属性,可以在 kendo 网格中使用。
{ field: "CountryName", title: "Country" },
您必须为自定义 属性 设置 NotMapped
属性:
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()
是下一个扩展方法:
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;
}