How to convert enum value to int in cshtml

You can simply cast it into int by placing (int) before the line code. Like:

@((int)sudhademo.CommonHelper.thing.Apple)

Here sudhademo is the namespace, CommonHelper is the class, thing is the name of enumerator.

See Full Example :

CommonHelper.cs

public class CommonHelper
{
public enum thing
{
None = 0,
Apple = 1,
Mango = 2,
Banana = 3,
Potato = 4,
Onion = 5,
Ladyfinger = 6
}
}




Index.cshtml


@{
var res = “”;
if (thingID == (int)sudhademo.CommonHelper.thing.Apple)
{ res = “It is an Apple”; }
else if (thingID == (int)sudhademo.CommonHelper.thing.Banana)
{ res = “It is a Banana”; }
else if (thingID == (int)sudhademo.CommonHelper.thing.Ladyfinger)
{ res = “It is a LadyFinger”; }
else if (thingID == (int)sudhademo.CommonHelper.thing.Mango)
{ res = “It is a Mango”; }
else if (thingID == (int)sudhademo.CommonHelper.thing.Onion)
{ res = “It is an Onion”; }
else if (thingID == (int)sudhademo.CommonHelper.thing.Potato)
{ res = “It is a Potato”; }
else
{ res = “Thing Unidentified!!!”; }
}

@res

Leave a Reply

Your email address will not be published. Required fields are marked *