【问题标题】:How to Display the name of a enum not the id in cshtml?如何在cshtml中显示枚举的名称而不是id?
【发布时间】:2022-11-01 21:34:28
【问题描述】:

我有一个这样的枚举:

using System.ComponentModel;
using System;
using System.Collections.Generic;
namespace eVote.Data.Enums
{
    public enum Positions
    {
        CEO =1,
        ProductManager,
         CTO,
         SalesManager,
        FinanceManager,
         HR,
        Accountant


    }
}

当我在 AddAsync 函数中使用此枚举时,我会这样使用:

 <div class="form-group">
        <label asp-for="position">Function </label>
        <select asp-for="position" class="form-control" asp-items="Html.GetEnumSelectList<Positions>()" asp-for="position"></select>
       
    </div>

但是,当我去索引时,显示的位置是位置的 id

&lt;td&gt;@candidate.position&lt;/td&gt;

有什么想法吗??

【问题讨论】:

    标签: c# model-view-controller razor enums controller


    【解决方案1】:

    如果要将枚举值的名称显示为字符串,则必须对该值调用 ToString() 方法。

    enum UserRole {
       User,
       Admin
    }
    
    Console.WriteLine(UserRole.User.ToString());
    

    这将打印User

    【讨论】:

      猜你喜欢
      • 2022-10-03
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      相关资源
      最近更新 更多