Probably so simple I'm missing it

I’m working on a site and I’ve created an enum which maps text strings for a user to an integer which is then stored in the database. I have the Edit page set up correctly so they can choose the proper text reason and store the integer in the db. However,
I want to also map it in the Index so the user can see the same text in the contact reason field there rather than the integer. (I didn’t design the tables and I can’t go back and change the fields by hand.)

So is there a relatively simple way to do this?

I believe you’re asking how to get the Enum name by value.

    class Program
    {
        enum Colors {Red, Green, Blue};

        static void Main(string[] args)
        {
            Console.WriteLine(Enum.GetName(typeof(Colors), 1));
        }
    }

If you want something that returns more than that you will need to create a custom attribute.  If that is what you are looking for, here is an old blog post about it.

http://weblogs.asp.net/stefansedich/enum-with-string-values-in-c

I think my headache is that I’m using the generated views from the controller and it’s all DB first in EF5 so I have a feeling it sounds like I’ve boxed myself in more on this.

Leave a Reply