无法将类型System.Collections.Generic.List <>转换为System.Collections.Generic.IEnumerable <>

时间:2019-05-04 14:42:40

标签: c# asp.net-mvc asp.net-core

我正在尝试使用HTML帮助程序从数据库中构建表单并在复选框中获取值。但是会弹出运行时错误,指出:

  

无法将列表<>转换为IEnumerable <>
  错误:   无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IEnumerable <>'。存在显式转换(您是否缺少演员表?)

我试图将List<>类型显式转换为IEnumerable<>,但错误仍然是InternalError

MembershipTypes = (IEnumerable<MembershipTypes>) membershipTypes  

是否将IEnumerable<> MembershipTypes转换为List<>,但没有运气

//The Controller Class
var membershipTypes = _context.MembershipTypes.ToList();
var viewModel = new NewCustomerviewModel { MembershipTypes = membershipTypes };
return View(viewModel);

//The NewCustomerViewModel Class (a ModelView Class)
public IEnumerable<MembershipTypes> MembershipTypes { get; set; }
public Customers Customers{ get; set; }

// THE VIEW CLASS
@Html.CheckBoxFor(
    m => m.Customers.MembershipTypes,
    new SelectList(Model.MembershipTypes,"Id", "Name"),
    "Select Membership Type",
    new { @class = "form-control" }
);

0 个答案:

没有答案