Razor converting enum to int inside a view

24,094

Solution 1

This syntax should do the trick (note the () instead of {}):

<option value='@( (Int16) PhoneType.Work )'>@PhoneType.Work</option>

Solution 2

Why not have another field on your viewModel that is an integer

public WorkId {get {return (int)Work; }

and use this in your view

<option value='@PhoneType.WorkId'>@PhoneType.Work</option>

Solution 3

We can use ChangeType function as below. Hope this helps for someone in future.

<option [email protected](PhoneType.Work, PhoneType.Work.GetTypeCode())>@PhoneType.Work</option>

or

<option [email protected](PhoneType.Work, typeof(int))>@PhoneType.Work</option>
Share:
24,094
Kuttan Sujith
Author by

Kuttan Sujith

Updated on August 18, 2020

Comments

  • Kuttan Sujith
    Kuttan Sujith almost 4 years

    I a razor view .I have line like bleow

    <option value='@{(Int16)PhoneType.Work}'>@PhoneType.Work</option>
    

    This is an option in a select list/dropdownlist In this I have an enum PhoneType. For text filed @PhoneType.Work works fine but for value field @{(Int16)PhoneType.Work is not working

    What can i do to get integer value of the enum at value field