【问题标题】:Generate radio button list from enum从枚举生成单选按钮列表
【发布时间】:2020-07-08 08:41:39
【问题描述】:

我有一个枚举,我想用它来自动填充单选按钮列表。我在按钮上获得了正确的值,但按钮旁边的标签与相应值的数字相同。我不确定在标签中放什么而不是 @source 这是我的枚举:

public enum SourceEnum
    {
       Youtube,
       Reddit,
       Instagram,
       Facebook,
       [Display(Name ="From Phone or Computer")]
       PhoneComputer,
       Other,
    }

还有我的 foreach 循环:

 @foreach (int source in (Timelineinfo.SourceEnum[])Enum.GetValues(typeof(Timelineinfo.SourceEnum)))
      {
        <div class="form-check">
           <input type="radio" asp-for="Timelineinfo.Source" class="form-check-input" value="@source">
           <label asp-for="Timelineinfo.Source" class="form-check-label">@source</label>
        </div>
      }

【问题讨论】:

    标签: c# html asp.net-core razor


    【解决方案1】:

    我通过将int source 更改为var source,然后将值更改为@((int)source) 解决了这个问题,所以我的代码现在看起来像:

     @foreach (var source in (Timelineinfo.SourceEnum[])Enum.GetValues(typeof(Timelineinfo.SourceEnum)))
          {
            <div class="form-check">
               <input type="radio" asp-for="Timelineinfo.Source" class="form-check-input" value="@((int)source)">
               <label asp-for="Timelineinfo.Source" class="form-check-label">@source</label>
            </div>
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多