I get a Short Diplay name and i need to get enum value using it?
[Display(Name = "Alabama", ShortName = "AL")]
Alabama = 1,
i just get AL from outside database. I need to read somehow my enum and get a proper value. Thanks for any help guys.
Thanks to help from the answers already given and from some additional research, I'd like to share my solution to this as an extension method in hope that it may help others:
You can use this extension as such:
If you are given the value
AL
, and you want to find the enum value that has that attribute, you can use a little bit of reflection to figure that out.Let's say our enum looks like this:
Here is a little code to get the
Foo
which has an attribute of ShortName = 'AL':@jasel i modify your code a little bit. this fits just fine what i need.
You can use the Enum.Parse or Enum.TryParse methods of the Enum class.
Sample: