【问题标题】:Cannot implicitly convert type SelectListItem to List无法将 SelectListItem 类型隐式转换为 List
【发布时间】:2019-09-04 17:17:44
【问题描述】:

视图模型:

public class ReportVM
{
    public WorkSheet worksheet { get; set; }
    public List<WorkReason> workreason { get; set; }
}

控制器:

public ActionResult ReportCreate(int? id)
    {
        var context = new Models.Context();

        var workSheets = context.WorkSheets.FirstOrDefault(ps => ps.WorkSheetId == id);
        var workReasons = context.WorkReasons.Select(w =>
            new SelectListItem()
            {
                Text = w.Name,
                Value = w.WorkReasonId.ToString()
            }
        ).ToList();
        var reportVM = new ReportVM
        {
            workreason = workReasons,
            worksheet = workSheets
        };
        return View(reportVM);
    }

我无法在视图模型中将工作原因从上下文填充到工作原因。任何建议如何解决?

workreason = workReasons - 我在这行有错误: 无法将类型“system.collections.generic.list(System.Web.Mvc.SelectListItem)”隐式转换为“system.collections.generic.list(ProjectE.Models.WorkReason)”

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    您正在尝试将 workReasons(类型列表)分配给属性 workreason(类型列表)。将您的虚拟机更改为以下内容:

    public class ReportVM
    {
        public WorkSheet worksheet { get; set; }
        public List<SelectListItem> workreason { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 2019-07-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多