Link to home
Start Free TrialLog in
Avatar of tesmc
tesmcFlag for United States of America

asked on

how to make enum accessible in view

i have
    public class ColorTypesConst
    {
        public enum Color
        {
            None,
            Red,
            Green,
            Blue,
            Yellow
        }
    }
      
However, my browser only shows the index position and not the actual value. This is what I have in my html

<tab heading="Color">
      <div>
            <span>  {{school.Color.Type}}  </span>
               </div>
</tab>

So all that gets displayed is "1" but I want to see "Red"
How do I do this in angular? I expect a key/value type of thing but couldn't find any example.

Please advise.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Call ToString on Type. That should print the name.
Avatar of tesmc

ASKER

school.Color.Type.ToString() did not return any results at all.
Avatar of tesmc

ASKER

i have the following class Color. which is why I was using .Type to access the enum

    public class Color
    {
        private string _shadow;
        private ColorTypesConst.Color _type = ColorTypesConst.Color.Color;

        //[JsonConverter(typeof(StringEnumConverter))]
        public ColorTypesConst.Color Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public string Shadow
        {
            get { return _shadow ; }
            set { _shadow = value; }
        }
    }
}

Open in new window

My mistake. I overlooked the Angular bits. You need to call ToString, but you need to do it at the point where you serialize into JavaScript. It might be easiest to just add a second property to your model which holds the string representation of the color value--i.e. a property which simply invokes ToString against your Type property.
Avatar of tesmc

ASKER

@käµfm³d  - can you show me example please?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tesmc

ASKER

hmm, that still didn't return string value for my enum Color.
Can you debug the JavaScript and see if there is a property called TypeDisplay on the serialized model?
Avatar of tesmc

ASKER

käµfm³d - i got it to work now. seems as though i hadn't saved my file yet.
Thanks so much for your help.
Avatar of tesmc

ASKER

thank you.