【发布时间】:2013-07-10 18:12:27
【问题描述】:
我正在使用 LINQ Self Join Query 在视图上显示数据。我的 sql 表包含一些员工详细信息。我需要显示员工详细信息及其经理姓名 因为它是表中的ManagerID为
EmpID 名称 ManagerID 名称 电话 地址 1 迈克 3 开发人员 123456 德克萨斯 2 大卫 3 RM 123456 德里 3 Roger NULL GM 123456 达拉斯 4 结婚 2 开发商 123456 NY 5 约瑟夫 2 开发商 123456 新加坡 7 Ben 2 开发商 123456 孟买 8 史蒂文 3 TL 123456 班格罗尔我需要把它改成名字
我的代码在控制器操作中
var emp = from m in t.Employees
join e1 in t.Employees on m.ManagerID equals e1.EmployeeID
select new { Id = m.EmployeeID ,
Name = m.Name,
Manager = e1.Name ,
Designation = m.Designation,
Phone =m.Phone ,address = m.Address };
return View(emp.Tolist());
在视图中
@model IEnumerable <mvc4application.models.employee>
但我收到运行时错误
传入字典的模型项是类型 System.Data.Objects.ObjectQuery
1[<>f__AnonymousType16[System.Int32,System.String, System.String,System.String,System.Nullable1[System.Int32],System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Mvc4application.Models.Employee]'。] System.Web.Mvc.ViewDataDictionary`1.SetModel(对象值) +405487
当然我理解这一点,因为我的观点是使用Mvc4application.Models.Employee type。
因为我无法将其转换为模型类型。
我们可以在MVC中使用SQL视图作为模型,以便我们可以在SQL中加入吗?
【问题讨论】:
标签: c# asp.net-mvc