In my ViewModel, I have a property that creates an enumeration for my form's drop down menu.
public enum Colors
{
[Description("Dark Red")]
DarkRed = 0,
[Description("Orange")]
Orange = 1,
[Description("Blue")]
Blue = 2
}
My Helper returns:
<select id="ddlColor">
<option value="DarkRed">Dark Red</option>
<option value="Orange">Orange</option>
<option value="Blue">Blue</option>
</select>
However, when I call the property in my model, I only get the name and not the value, e.g. DarkRed and not 0.
model.Selections = InsertForm(model.Color);
How can I cast this in my model reference so I get the value from the enum?
You may have to cast the value like this:-
var value = (int)model.Color;
NOTE:-
All enumeration type have an underlying type, which can be any integral type except char.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With