0

In an MVC 5 app, I need to get an Enums's Display Name attributes as a list. I can use the Enum.GetNames() to get the names no problem, but can't figure out how to implement something like Enum.GetDisplayName(). I've tried extension methods and searched the web, but no success yet.

How can I do this?

This is not a duplicate of this:

How to get the Display Name Attribute of an Enum member via MVC razor code? 9 answers

Controller:

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

View/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);
            }
        });

Viewmodel/Class:

public enum Claim
    {
        [Display(Name = "Benefit Verification")]
        BenefitVerification,
        [Display(Name = "Claim Appeal")]
        ClaimAppeal        
    }
Big Daddy
  • 5,160
  • 5
  • 46
  • 76
  • have a look here http://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code – Sim1 Mar 13 '15 at 15:09
  • I looked at that. It's geared towards Razor. I need a solution that returns the display names from the controller as a JsonResult if possible. Thanks. – Big Daddy Mar 13 '15 at 15:11
  • This is not a duplicate. I think it's very a different approach. – Big Daddy Mar 13 '15 at 15:17
  • have you tried with this code? public static string GetDisplayName(this Enum enumValue) { return enumType.GetType().GetMember(enumValue.ToString()) .First() .GetCustomAttribute() .Name; } – Sim1 Mar 13 '15 at 15:21
  • I think it's pretty bad that someone can cavalierly mark a question as a duplicate without taking the time to understand a) what's really being asked and b) does the referenced "already answered" question address the poster's question? I've seen this done on other posts, usually by the same people, and think it's counter productive. Ohter already answered questions may not be clear to others and can be explained in a different way. I think it's all about helping people, not rigidity and flexing your head. – Big Daddy Mar 13 '15 at 15:25
  • @SimoneRiboldi...Thanks, but I need a list of values for the entire enum. – Big Daddy Mar 13 '15 at 15:36
  • The duplicate does show you how to do this. The accepted answer has all the extension methods you need - in your case its `return Json(EnumHelper.GetDisplayValues(Claims.BenefitVerification, JsonRequestBehavior.AllowGet);` which will return `{ "Benefit Verification", "Claim Appeal" }`, although you could adapt the extension method so that `GetDisplayValues` uses the type rather than having to pass an enum value as a parameter. –  Mar 14 '15 at 00:00
  • @StephenMuecke...Thanks for your comment, it helped me. I got caught-up in the razor code and couldn't see the solution. – Big Daddy Mar 16 '15 at 12:24

0 Answers0