I have the following line
<div class="row" data-hns-target="salesChannel" data-hns-condition="^@Model.SeenSalesChannel$">
Model.SeenSalesChannel is an enumeration. I would like to convert this to the corresponding int value but
<div class="row" data-hns-target="salesChannel" data-hns-condition="^@(int)Model.SeenSalesChannel$">
is not valid. What else could I do?
Here you will learn how to convert Int to Enum in C#. You can explicitly type cast an int to a particular enum type, as shown below. Use the Enum.ToObject () method to convert integers to enum members, as shown below. How to loop through an enum in C#?
And enums can have different underlying types, such as int, short, long, byte. If you know the actual enum type, you can cast it to that specific type directly. If you don't know the actual enum type, or you deal with a generic type, you can use GetTypeCode () method to check the enum type while converting.
As you see enum object is used to store two types of values - one type stores index and enum strings, other type stores reverse of data like string and enum. By default, each enum constants is assigned with numbers starting from 0,1 ie.
enum MyEnum { First = 1, Second = 2, Third = 3 }; string enumStr = MyEnum.First.ToString (); enum MyEnum { First = 1, Second = 2, Third = 3 }; var myEnum = MyEnum.Second; string enumStr = Convert.ToDecimal (myEnum).ToString ("0");
Wrap the whole thing in ()
<div class="row" data-hns-target="salesChannel" data-hns-condition="^@((int)Model.SeenSalesChannel)$">
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