Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 如何转换IEnumerable<;对象>;到IEnumerable<;。。。MVC.Rendering.SelectListItem>;asp.net内核_Asp.net Core - Fatal编程技术网

Asp.net core 如何转换IEnumerable<;对象>;到IEnumerable<;。。。MVC.Rendering.SelectListItem>;asp.net内核

Asp.net core 如何转换IEnumerable<;对象>;到IEnumerable<;。。。MVC.Rendering.SelectListItem>;asp.net内核,asp.net-core,Asp.net Core,我试图在我的页面上显示一个带有标题列表的下拉列表。我的目标是 public class Title { public int Id { get; set; } [Display(Name = "Title")] public string Description { get; set; } } 我有一个包含线的视图模型- public IEnumerable<Title> Titles { get; } public IEnumera

我试图在我的页面上显示一个带有标题列表的下拉列表。我的目标是

public class Title
{
    public int Id { get; set; }

    [Display(Name = "Title")]
    public string Description { get; set; }
}
我有一个包含线的视图模型-

public IEnumerable<Title> Titles { get; }
public IEnumerable Titles{get;}
在我的页面上我有-

<select  asp-for="Person.TitleId" asp-items="@Model.Titles"/>

但我得到了一个错误:

无法将类型“IEnumerable”隐式转换为IEnumerable

你如何转换

谢谢我找到了答案-

 var listOFTitles = _titleRepository.Titles.Select(i => new SelectListItem()
            {
                Text = i.Description.ToString(),
                Value = i.Id.ToString()
            });

找到了解决办法。选择(i=>newselectListItem(){Text=i.Description.ToString(),Value=i.Id.ToString()});是的,您应该使用selectListItem来使用这种形式的下拉列表,如果有帮助,您可以将答案设置为答案