需要枚举的显示名称作为列表

时间:2015-03-13 15:05:44

标签: c# asp.net-mvc enums extension-methods

MVC 5应用中,我需要将Enums的Display Name属性作为列表。我可以使用Enum.GetNames()来获取名称没问题,但无法弄清楚如何实现类似Enum.GetDisplayName()的东西。我尝试过扩展方法并在网上搜索,但还没有成功。

我该怎么做?

这不是重复:

如何通过MVC razor代码获取Enum成员的显示名称属性? 9个答案

控制器:

public JsonResult GetCategoryDescriptions(int id)
        {
            var category = (Category)id;
            switch (category)
            {
                case Category.Claim:
                    return Json(Enum.GetNames(typeof(Claim)).ToList(),JsonRequestBehavior.AllowGet);
                case Category.Commissions:
                    return Json(Enum.GetNames(typeof(Commissions)).ToList(), JsonRequestBehavior.AllowGet);              
                default:
                    return null;
            }
        }

查看/ JQuery的:

$("#category").change(function () {
            if ($("#category").val() != "Select") {
                var val = $("#category").val();
                $.ajax({
                    url: "@Url.Action("GetCategoryDescriptions","Call", new {area = "", id = "_id_"})".replace("_id_", val),
                    type:"GET",
                    datatype:"json",
                    contentType:"application/json"
                }).done(function (descriptions) {
                    $("#categoryDescription").empty();
                    $("#categoryDescription").append("<option> Select </option>");
                    for (var i = 0; i < descriptions.length; i++) {
                        $("#categoryDescription").append("<option>" + descriptions[i] + "</option>");
                    }

                }).fail(function(jqXHR, textStatus){ 
                    alert("Error getting Category Descriptions"); 
                });
            }
            else {
                $("#categoryDescription").empty();
                $("#categoryDescription").prop("disabled", true);
            }
        });

视图模型/类别:

public enum Claim
    {
        [Display(Name = "Benefit Verification")]
        BenefitVerification,
        [Display(Name = "Claim Appeal")]
        ClaimAppeal        
    }

0 个答案:

没有答案