[英]Pass List To View C# Linq
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();
嘗試這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.