【问题标题】:simple asp.net tag helper list<> question简单的 asp.net 标签助手列表<> 问题
【发布时间】:2018-11-25 15:25:07
【问题描述】:

我不确定你是否能做到这一点,我只是在学习和尝试一些。

在我的一个 cshtml 文件中:

@{ 
List<string> items = new List<string>();
items.Add("item 1");
items.Add("item2");

}

然后我只想在选择中访问它:

            <td><select asp-for="Genre" asp-items=@items /></td>

有没有办法做到这一点,或者我只是想把棒球喂给真正想要苹果的马?

【问题讨论】:

    标签: c# asp.net-core tag-helpers


    【解决方案1】:

    您可以尝试使用List&lt;SelectListItem&gt; 而不是List&lt;string&gt;

    SelectListItem 类属于 Microsoft.AspNet.Mvc.Rendering 命名空间。

    @{ 
    
        List<SelectListItem> items= new List<SelectListItem>()
        {
            new SelectListItem {Text = "item1", Value = "item1"},
            new SelectListItem {Text = "item2", Value = "item2"}
        };
    }
    
    <td><select asp-for="Genre" asp-items="@items"></select></td>
    

    【讨论】:

    • 我得到相同的错误(我应该发布)无法将类型“字符串”隐式转换为 System.Collections.Generic.IEnumerable '跨度>
    • 这很有效。谢谢你,我很感激。当 SOF 允许我时,我会接受它......
    猜你喜欢
    • 1970-01-01
    • 2013-10-06
    • 2019-09-26
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多