33

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

dove
  • 20,469
  • 14
  • 82
  • 108
Kuttan Sujith
  • 7,889
  • 18
  • 64
  • 95

3 Answers3

66

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

<option value='@( (Int16) PhoneType.Work )'>@PhoneType.Work</option>
Stéphane Bebrone
  • 2,713
  • 17
  • 18
3

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>
dove
  • 20,469
  • 14
  • 82
  • 108
1

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

<option value=@Convert.ChangeType(PhoneType.Work, PhoneType.Work.GetTypeCode())>@PhoneType.Work</option>

or

<option value=@Convert.ChangeType(PhoneType.Work, typeof(int))>@PhoneType.Work</option>
vineel
  • 3,483
  • 2
  • 29
  • 33