DropDownList issue with Enums

Viewed 68

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:

  • foo = Owner
  • Moderator
  • Janitor
  • Designer
  • Submitter

But I want to show it looks like:

  • foo = Sahip
  • Moderatör
  • Temizlikçi
  • Boyacı
  • Paylaşımcı

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.

2 Answers
Related