简体   繁体   English

将Enum值用作下拉列表ASPCore中的值

[英]Use Enum value for a value in a dropdownlist ASPCore

Im trying to use a value from an enum I've declared in my code previously 我试图使用我之前在代码中声明的枚举中的值

Here is the enum : 这是枚举:

public enum CompanyStatus
{
    WaitingForApproval = 1,
    Approved = 2,
    Disapproved = 3
}

Here is the select list where I am trying to use that Id that has been already declared 这是我要使用已声明的ID的选择列表

<select id="CompanyStatusFilter" validateme="true" class="form-control fullWidth" placeholder="Status" data-validation="length alphanumeric" data-validation-length="min1">
                            <option value=""></option>
                            <option value="(int)Core.Engine.CompanyStatus.Approved">Approved</option>
                            <option value="@Html.GetEnumSelectList<CompanyStatus>">Waiting for approval</option>                                
                        </select>

I don't want to explicitly type in the id as the value. 我不想明确输入id作为值。 Thanks in advance 提前致谢

IMHO it would be better if you used the helper for Enums for example: 恕我直言,如果您使用Enums的帮助程序,例如:

@Html.DropDownListFor(m => m, Html.GetEnumSelectList(Model.GetType()))

and on the Enum values you can use the [Display(Name = "Test 1")] attribute to control what gets shown. 在Enum值上,可以使用[Display(Name = "Test 1")]属性来控制显示的内容。

You can use GetEnumSelectList to render a select for the enum values. 您可以使用GetEnumSelectList呈现枚举值的选择。

<select name="CompanyStatus" asp-items="Html.GetEnumSelectList(typeof (CompanyStatus))">
</select>

Reference 参考

我能够通过将其转换为Int32来获取枚举的ID

<option value="@Convert.ToInt32(Core.Engine.CompanyStatus.WaitingForApproval)">Waiting for approval</option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM