Casting error: The type IList cannot be implicitly converted to IEnumerable
Casting error: The type IList cannot be implicitly converted to IEnumerable
我的 EF Codefirst 模型是 Lib
public class Lib
{
public int ID { get; set; }
[Required]
[PageRemote(PageHandler = "IsKeyExists", HttpMethod = "Get")]
public string Key { get; set; }
}
我想要一个 combobox
in razor 页面到 select 一个库。
我的 cshtml 代码是:
<select asp-for="Lib" asp-items="Model.Libs"></select>
我的 PageModel 代码是:
public IList<Lib> Libs { get; set; }
public Lib Lib { get; set; }
然后我得到一个编译器错误
error CS0266: 无法将类型“System.Collections.Generic.IList”隐式转换为“System.Collections.Generic.IEnumerable”。存在一个显式转换(是否缺少强制转换?)
英文版(来自Google翻译):
error CS0266: The type "System.Collections.Generic.IList
" cannot be implicitly converted
to "System.Collections.Generic.IEnumerable
". There is an
explicit conversion (is a cast missing?)
在wpf combobox中有一个属性,名字叫DisplayMemberPath
,有类似的方法吗?
您需要传递兼容的类型。如果您查看 docs,您会发现他们使用 SelectListItem
列表。您可以将 public IList<Lib> Libs{get;set;}
重构为 public IList<SelectListItem> Libs{get;set;}
另一种方法是将您的列表映射到 SelectListItem
列表:
@using System.Collections.Generic
<select asp-for="Lib" asp-items="Model.Libs.Select(l=> new SelectListItem{Text=l.Key, Value=l.ID.ToString()})"></select>
我的 EF Codefirst 模型是 Lib
public class Lib
{
public int ID { get; set; }
[Required]
[PageRemote(PageHandler = "IsKeyExists", HttpMethod = "Get")]
public string Key { get; set; }
}
我想要一个 combobox
in razor 页面到 select 一个库。
我的 cshtml 代码是:
<select asp-for="Lib" asp-items="Model.Libs"></select>
我的 PageModel 代码是:
public IList<Lib> Libs { get; set; }
public Lib Lib { get; set; }
然后我得到一个编译器错误
error CS0266: 无法将类型“System.Collections.Generic.IList”隐式转换为“System.Collections.Generic.IEnumerable”。存在一个显式转换(是否缺少强制转换?)
英文版(来自Google翻译):
error CS0266: The type "System.Collections.Generic.IList " cannot be implicitly converted to "System.Collections.Generic.IEnumerable ". There is an explicit conversion (is a cast missing?)
在wpf combobox中有一个属性,名字叫DisplayMemberPath
,有类似的方法吗?
您需要传递兼容的类型。如果您查看 docs,您会发现他们使用 SelectListItem
列表。您可以将 public IList<Lib> Libs{get;set;}
重构为 public IList<SelectListItem> Libs{get;set;}
另一种方法是将您的列表映射到 SelectListItem
列表:
@using System.Collections.Generic
<select asp-for="Lib" asp-items="Model.Libs.Select(l=> new SelectListItem{Text=l.Key, Value=l.ID.ToString()})"></select>