Razor将enum转换为视图内的int

时间:2021-04-12 16:22:39

I a razor view .I have line like bleow

我是一个剃刀的观点。我有一线像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

这是选择列表/下拉列表中的一个选项。在此我有一个枚举PhoneType。对于提交的文本@ PhoneType.Work工作正常,但对于值字段@ {(Int16)PhoneType.Work无法正常工作

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

我该怎么做才能在值字段中获取枚举的整数值

3 个解决方案

#1


44  

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

这个语法应该可以解决问题(注意()而不是{}):

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

#2


2  

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

为什么不在viewModel上有一个整数的另一个字段

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

and use this in your view

并在您的视图中使用它

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

#3


1  

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

我们可以使用ChangeType函数,如下所示。希望这有助于将来的某些人。

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

or

要么

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

#1


44  

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

这个语法应该可以解决问题(注意()而不是{}):

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

#2


2  

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

为什么不在viewModel上有一个整数的另一个字段

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

and use this in your view

并在您的视图中使用它

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

#3


1  

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

我们可以使用ChangeType函数,如下所示。希望这有助于将来的某些人。

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

or

要么

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