-2

I have an enum which i use in my app as follows:

  public enum TaskStatus{
       WaitinForApproval,
       SubmittedForReview,
       .....
  }

When i display this on the page it displays :

SubmittedForReview

I would like to show it as

Submitted For Review

How this can be done without too much hassle?

DarthVader
  • 52,984
  • 76
  • 209
  • 300

5 Answers5

1

Create some HTML Helper and Call it whenever you like

public static string ShowMyEnumTitle(this HtmlHelper helper, myEnum enumTitle)
        {
            string enumText = "";
            string result = "";
            enumText = String.Format("{0}", Enum.GetName(typeof(myEnum), enumTitle));
            for (int i = 0; i < enumText.Length; i++)
            {
                if ((int)enumText[i] >= 65 && (int)enumText[i] <= 90 && i != 0)
                {
                    result += " " + enumText[i];
                }
                else
                {
                    result += enumText[i];
                }
            }
            return result;
        }

this helper is separating the text based on the capital characters so your text from "SubmittedForReview" will be convert to "Submitted For Review", and then you can call this html helper in your view:

@Html.ShowMyEnumTitle(Model.myEnumMember)
Armen
  • 1,083
  • 2
  • 10
  • 18
0

This is not really a good way of doing it, but should work.

You can make a Dictionary<TaskStatus, string>, and populate the values with what you want to display.

Then, before you show the value, just use the dictionary to lookup what value to show.

Dictionary<TaskStatus, string> myDictionary = new Dictionary<TaskStatus, string>();
myDictionary.Add(TaskStatus.WaitinForApproval, "Waiting For Approval");

string displayValue = myDictionary[TaskStatus.WaitinForApproval]; //Waiting For Approval

You will have to update this if you ever change your enum.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
0

You can use data annotations.

using System.ComponentModel.DataAnnotations;

public enum TaskStatus{
   [Display(Name = "Waiting for approval")]
   WaitinForApproval,
   [Display(Name = "Submitted for review")]
   SubmittedForReview,
   .....
  }

This is useful anywhere in your program where you want to use a friendly name as opposed to a member's actual name. Make sure you are referencing System.ComponentModel.DataAnnotations in your project before you create the using statement.

EDIT

In regards to everyone else's answers if you use Description attribute you need to access it using reflection. Using the display attribute automatically shows your desired string in the UI.

Community
  • 1
  • 1
Adrian
  • 3,332
  • 5
  • 34
  • 52
-1

You could use attributes to assign a string to every enum item.
For example:

public enum TaskStatus{
   [Description("Waiting for Approval!!")]
   WaitinForApproval,
   [Description("Submmited For Review.")]
   SubmittedForReview,
   [Description("... w/e")]
   .....
}

and then access it using reflection:

public static string GetEnumDescription(Enum value) {
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = 
        (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
        return attributes[0].Description;
    else
        return value.ToString();
}


Usage:

GetEnumDescription(TaskStatus.SubmittedForReview);

Source: http://blog.spontaneouspublicity.com/associating-strings-with-enums-in-c

NucS
  • 619
  • 8
  • 21
-1

If you always want capital letters to represent word breaks, you can use a simple regex replace to add spaces.

    private static string EnumToString(TaskStatus status)
    {
        return Regex.Replace(status.ToString("F"), "(?<=\\w)([A-Z])", " $1");
    }

This expression matches any capital letter that is preceded by any letter (the lookbehind is so you don't add a space at the beginning). You could easily make this an extension if you wanted as well.

 Console.WriteLine( EnumToString( TaskStatus.SubmittedForReview ) ); // "Submitted For Review"

I believe this approach to be superior to reflection and attributes because it also doesn't require any additional work when/if you add more enum values. The downside is that the output value is directly related to the enum value and can't be more complex or include punctuation.

Oren Melzer
  • 749
  • 4
  • 7