无法显示在剑道电网枚举值 [英] Cannot display enum values on Kendo grid

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

问题描述

在我MVC5应用程序,我有一个枚举类,如下图所示,用这种方法我可以通过枚举值,即美国,英国,而不是美国,从控制器到视图。我如何传递和显示枚举说明具有以下办法?我试过很多不同的解决方法, C#字符串枚举等,但他们没有得到解决。我的问题。另一方面,我不希望使用密封类,这将是对我用枚举类的解决方案更好,如下所示:


枚举:

 公开枚举国家
{
    [说明(美国)]
    美国= 1,
    [说明(联合王国)]
    英国= 2,
    [说明(新西兰)]
    NewZealand的买家= 3,
    [说明(法国)]
    法国= 4,
    [说明(德国)]
    德国= 5
}



型号:

 公共类VisitorViewModel
{
    [键]
    公众诠释VisitorID {搞定;组; }    公开国家国家{搞定;组; }
    // code不再赘述
}



控制器:

 公共JsonResult Visitor_Read([DataSourceRequest] DataSourceRequest要求)
{
    VAR的结果= db.Visitors.Select(M = gt;新建VisitorViewModel
    {
        VisitorID = m.VisitorID,
        国家= m.Country
        // code不再赘述
    })
    VAR jsonResult = JSON(结果,JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;
    返回jsonResult;
}



查看:

  $(文件)。就绪(函数(){    VAR电网= $(#visitorGrid)。kendoGrid({
        数据源: {
            类型:JSON
            运输: {
                阅读:{
                    网址:/观众/ Visitor_Read
                    数据类型:JSON
                    缓存:假的
                }
            },
            模式:{
                型号:{
                    字段:{
                        VisitorID:{类型:'数'},
                        国家:{类型:'字符串'}
                    }
                }
            }
        },
        列:
        [
            {场:VisitorID,标题:ID},
            {场:国家,标题:国家},
        ]
    })的数据(kendoGrid);});


解决方案

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

 使用System.ComponentModel.DataAnnotations.Schema;
公共类VisitorViewModel
{
    [键]
    公众诠释VisitorID {搞定;组; }    公开国家国家{搞定;组; }    [NotMapped]
    公共字符串国家或地区名称
    {
        {返回Country.GetDescription(); }
    }
}

GetDescription()旁边的扩展方法:

 公共静态字符串GetDescription(此枚举E)
{
    变种字段= e.ToString();
    var属性= e.GetType()getfield命令(场).GetCustomAttributes(typeof运算(DescriptionAttribute),FALSE).FirstOrDefault()。    返回属性!= NULL? ((DescriptionAttribute)属性)。说明:现场;
}

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

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

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