Kuttan Sujith
Kuttan Sujith

Reputation: 7979

Razor converting enum to int inside a view

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

Upvotes: 34

Views: 27435

Answers (3)

vineel
vineel

Reputation: 3683

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>

Upvotes: 2

St&#233;phane Bebrone
St&#233;phane Bebrone

Reputation: 2763

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

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

Upvotes: 69

dove
dove

Reputation: 20674

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>

Upvotes: 3

Related Questions