【问题标题】:asp.net razor: items from list in model are not displayed in dropdownasp.net razor:模型中列表中的项目未显示在下拉列表中
【发布时间】:2023-01-12 22:29:21
【问题描述】:

我正在尝试在下拉列表中显示模型中列表中的项目。

我试过了:

<select asp-for="Requeststatus" asp-items="@Model.ThemeLinkingType"></select>

这将呈现一个下拉列表,但列表为空。

但是,如果我尝试:

 @{
     foreach (var item in Model.ThemeLinkingType)
     {
         <p>@item.DisplayName</p>
     }

 }

我从我的列表中得到我的项目返回一个漂亮的小(不是下拉)有点列表。

这是吸气剂:

public List<ThemeLinkingType> ThemeLinkingType
{
    get
    {
        var result = new List<ThemeLinkingType>();


        foreach(var item in Enum.GetValues(typeof(ItemType)))
        {
            var name = item.ToString();
            var id = (int)item;

            var itemToAdd = new ThemeLinkingType
            {

               InternalName = name,
               Id = id,
               DisplayName = GetDisplayName(name)
            };

            result.Add(itemToAdd);
        }

        return result; 
    }
}

请有人向我解释这个问题。

谢谢!

【问题讨论】:

    标签: c# asp.net razor


    【解决方案1】:

    您需要使用 SelectList 类型数据设置 asp-items

    @{
        var l = new SelectList(Model.ThemeLinkingType, "Id","DisplayName");
    }
    <select asp-for="Requeststatus" asp-items="@l"></select>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多