【发布时间】:2016-01-21 15:20:19
【问题描述】:
我在下面遇到一个异常:
传入字典的模型项的类型为“System.Collections.Generic.List
1[DropDownList.Models.Department]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[DropDownList.Models.Employee]”。
这是我的模型, Employee.cs:
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
部门.cs:
public class Department
{
public int DepartmentId { get; set; }
public string DeptCode { get; set; }
public string DepartmentName { get; set;}
public string Syscode { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
还有我的EmployeesController的索引动作,异常产生的地方:
public ActionResult Index()
{
var employees =
from dept in db.Departments
select new
{
dept,
depts = from emp in dept.Employees
where dept.Syscode == "isols123"
select dept
};
var employs = employees
.AsEnumerable()
.Select(m => m.dept);
return View(employs.ToList());
}
这是我的索引视图:
@model IEnumerable<DropDownList.Models.Employee>
.
.
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Department.DepartmentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmployeeName)
</td>
</tr>
}
出了什么问题,我不知道,我严重卡在这个问题上 :(,任何人都请带我离开这里,在此先感谢..
【问题讨论】:
标签: asp.net-mvc linq razor collections