Enes Chufy
Enes Chufy

Reputation: 63

DropDownList issue with Enums

I'm currently localizing an open source project and I have an issue with Enums.

Here is my Enums.cs (not the whole file, just part of where the issue appears).

 public enum ModeratorLevel
    {
        [Display(Name = "Sahip")]
        Owner = 1,
        [Display(Name = "Moderatör")]
        Moderator = 2,
        [Display(Name = "Temizlikçi")]
        Janitor = 3,
        [Display(Name = "Boyacı")]
        Designer = 4,
        [Display(Name = "Paylaşımcı")]
        Submitter = 99
    }

and this is my View (again, not the whole file, contains only the codes that I want to fix)

@{
    string subverseName = ViewBag.SubverseName;

    var levelsAvailable  = Enum.GetValues(typeof(ModeratorLevel)).OfType<ModeratorLevel>();
    var currentLevel = ModeratorPermission.Level(User.Identity.Name, subverseName);
    levelsAvailable = levelsAvailable.Where(x => x > currentLevel || currentLevel == ModeratorLevel.Owner);

}

@* these areas contain other codes is not related to my issue *@

        <div class="form-group">
            @Html.Label("foo", htmlAttributes: new {@class = "control-label col-md-2"})
            <div class="col-md-4">
                @Html.DropDownListFor(model => model.Power, levelsAvailable.Select(x => new SelectListItem() { Text = x.ToString(), Value = ((int)x).ToString() }), new { @class = "form-control"} )
                @Html.ValidationMessageFor(model => model.Power, "", new {@class = "text-danger"})
            </div>
        </div>

@* these areas contain other codes is not related to my issue *@

and this is output; dropdownlist

aaand now, What I want? I want to display DisplayNames instead Enum values. The outpot looks like:

But I want to show it looks like:

But in this change, changes must be only visual. We shouldn't change any code that affects to Enum's function. I just want to show only DisplayNames instead of Enum values.

Thanks.

Upvotes: 3

Views: 80

Answers (2)

Fabiano
Fabiano

Reputation: 5194

Use the EnumDropDownListFor helper

@Html.EnumDropDownListFor(model => model.Power)

Upvotes: 0

Mustapha Larhrouch
Mustapha Larhrouch

Reputation: 3393

you can add an extension method :

public static string DisplayName(this Enum value)
    {
        Type enumType = value.GetType();
        var enumValue = Enum.GetName(enumType, value);
        MemberInfo member = enumType.GetMember(enumValue)[0];

        var attrs = member.GetCustomAttributes(typeof(DisplayAttribute), false);
        var outString = ((DisplayAttribute)attrs[0]).Name;

        if (((DisplayAttribute)attrs[0]).ResourceType != null)
        {
            outString = ((DisplayAttribute)attrs[0]).GetName();
        }

        return outString;
    }

and then use it in the view :

@Html.DropDownListFor(model => model.Power, levelsAvailable.Select(x => new SelectListItem() { Text = x.DisplayName(), Value = ((int)x).ToString() }), new { @class = "form-control"} )

Upvotes: 1

Related Questions