传递列表以查看 C# Linq
本文关键字:Linq 列表 | 更新日期: 2023-09-27 18:15:50
public ActionResult Index()
{
ESSEntities DB = new ESSEntities();
List<EmployeeMdl> EmpList = DB.Employees.ToList();
return View(EmpList);
}
如何传递此列表以查看,因为我遇到错误
无法将类型"隐式转换为"System.Collections.Generic.List"
你可以做这样的事情。
public ActionResult Index()
{
ESSEntities DB = new ESSEntities();
List<Employees> lstDBEmployees = DB.Employees.ToList();
List<EmployeeMdl> EmpList = new List<EmployeeMdl>();
foreach(var thisEmployee in lstDBEmployees )
{
EmpList.Add(new EmployeeMdl(){
prop1 = thisEmployee.prop1,
prop2 = thisEmployee.prop2
});
}
return View(EmpList);
}
我认为您的观点没有收到列表,请使用这个。
视图:
@model List<EmployeeMdl>
@foreach(var item in Model)
{
//code here
}
控制器:
public ActionResult Index()
{
ESSEntities DB = new ESSEntities();
List<EmployeeMdl> EmpList = DB.Employees.ToList();
return View(EmpList);
}
var EmpList =
DB.Employees.Select(c => new{ /*your model properties somthing like c.EmployeeId, c.EmployeeName*/}).ToList();
试试这个。