1

Is there a way to get DisplayName (Text) from EnumDropDownListFor helper for enum?

Enum:

public enum PartnersGroup
{
   [Display(Name="Partner_SystemsGroup",ResourceType=typeof(Global) )]
    SystemsGroup,
   [Display(Name="Partner_SoftwarePartners",ResourceType=typeof(Global))]
    SoftwarePartners,
   [Display(Name="Partner_IntegrationPartners",ResourceType=typeof(Global))]
    IntegrationPartners,

}

Model

public class Partner
{
    public PartnersGroup PartnersGroup { get; set; }
}

Controller

// GET: Partners/Create
public ActionResult Create()
{
    ----
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Partner partner)
{
    if (ModelState.IsValid)
    {
        // here model does not show language based text(shown in dropdown in view). Getting Enum value like "IntegrationPartners".
        // In model parner.ParnerGroup shows IntegrationPartners, but it should be "Integration Partners" in English or "Partenaires d'intégration" in French.
         // Save to DB
    }        
}

View (Create) is displaying language based resource values as expected for keys. It is working fine.

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

Any help please?

kpr
  • 451
  • 5
  • 17
  • possible duplicate of [How to get the Display Name Attribute of an Enum member via MVC razor code?](http://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code) – Zohar Peled Jun 15 '15 at 12:42
  • If you use MVC 5.1.2 `EnumDropDownListFor` extension method must work for you. See http://www.asp.net/mvc/overview/releases/mvc51-release-notes – alisabzevari Jun 15 '15 at 12:47
  • The `DisplayAttribute` is an attribute specifically for use in a view. What possible use do you have for accessing it in the controller? –  Jun 15 '15 at 12:57
  • 1
    @Zohar, No its not duplicate. I want to use EnumDropDownListFor helper only. Thanks – kpr Jun 15 '15 at 13:27
  • @alisabzevari, I used it like this in Get Create Action Method: ` ViewBag.Groups = EnumHelper.GetSelectList(typeof(PartnersGroup), PartnersGroup.LabelPartners); ` And in view I used DropdownHelper. ` @Html.DropDownList("Groups", null, htmlAttributes: new { @class = "form-control" }) ` . But it not giving selected value though. I might have missed something. Thanks – kpr Jun 15 '15 at 13:37
  • @Stephen. PartnerGroup (Enum) is auto generated as field since I used it in Partner model. So in view Its just bound and giving selected item on post for EnumDropdownHelper. – kpr Jun 15 '15 at 13:48
  • @kpr, Which is exactly what its supposed to so . What is the reason you would want the views display text in the POST method? –  Jun 15 '15 at 13:51
  • @stephen. Enum dropdown is culture based and its showing items correctly in particular language. But the value on post is plain selected value of Enum. For Example In English If selected Item is "Software Partner" then In model on post, it comes as "SoftwarePartner" (Without spaces) . – kpr Jun 15 '15 at 14:00
  • Of course it does. For the 3rd time, what possible reason do you have for need the display text in the controller? –  Jun 15 '15 at 14:01

2 Answers2

1

You can write an extension method for Enums to return Display value of an Enum value:

public static class DataAnnotationHelpers
{
    public static string GetDisplayValue(this Enum instance)
    {
        var fieldInfo = instance.GetType().GetMember(instance.ToString()).Single();
        var descriptionAttributes = fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false) as DisplayAttribute[];
        if (descriptionAttributes == null) return instance.ToString();
        return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].GetName() : instance.ToString();

    }

}

And Use it like this:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Partner partner)
{
    if (ModelState.IsValid)
    {
         var localizedDisplayName = partner.PartnersGroup.GetDisplayValue();
         // Save to DB
    }        
}
alisabzevari
  • 8,008
  • 6
  • 43
  • 67
  • Thanks alisabzevari ! It worked as the solution of problem, without adding an extra property in 'Partner' model as Bellash suggested (Though I didn't use that code). – kpr Jun 15 '15 at 14:34
0

Approximate solution is add PartnersGroupString property getter in the partner class in order to transform enum into their localized strings. See bellow code

public class Partner
{
   public PartnersGroup PartnersGroup { get; set; }

   [doNotGenerateIntoDatabase]
   public string PartnersGroupString { 
  get{ 
        return  
               Strings.ResourceManager.GetString("PartnersGroup_" + this.PartnersGroup);}
   }

}

then you can use this property in your controller using ...model.PartnersGroupString

Bellash
  • 7,560
  • 6
  • 53
  • 86