tesmc
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.
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.
Call ToString on Type. That should print the name.
ASKER
school.Color.Type.ToString () did not return any results at all.
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; }
}
}
}
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.
ASKER
@käµfm³d - can you show me example please?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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.
Thanks so much for your help.
ASKER
thank you.