【问题标题】:Populating a dropdownlist with enum使用枚举填充下拉列表
【发布时间】:2021-05-12 12:27:39
【问题描述】:

我正在尝试使用 enum 值填充下拉列表,但我遇到了问题。这是我的课

public class Project
    {
        ...
        public Currencies Money { get; set; }

        public enum Currencies
        {
            [Display(Name = "Euro")]
            Euro = 1,
            [Display(Name = "USD")]
            USD = 2
        }

这是我的观点

@model Projects.Domain.Project

@{
    ViewData["Title"] = "Create";
}

<br />
<p style="font-weight:bold;">Add new project</p>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">            
            <div class="form-group">
                <select asp-for="Money" asp-items="@Html.GetEnumSelectList<Currencies>()"></select>
            </div>
        </form>
    </div>
</div>

但我收到一条错误消息,提示 The type or namespace currencies could not be found. 如果我将 Currencies 替换为 Money,则结果相同。填充下拉列表的正确方法是什么?

【问题讨论】:

  • 要么使用 Currencies 的完全限定名称,要么为其命名空间添加 using 语句。

标签: c# html dropdown


【解决方案1】:

您可以尝试使用全名:Projects.Domain.Project.Currencies

选择代码:

 <select asp-for="Money" asp-items="@Html.GetEnumSelectList<Projects.Domain.Project.Currencies>()"></select>

或者使用命名空间:

@using Projects.Domain

选择代码:

 <select asp-for="Money" asp-items="@Html.GetEnumSelectList<Project.Currencies>()"></select>

【讨论】:

    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    相关资源
    最近更新 更多