ASP .NET MVC SELF加入视图查询 [英] ASP .NET MVC SELF Join query for view

查看:74
本文介绍了ASP .NET MVC SELF加入视图查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

请帮助





我正在使用LINQ Self Join Query在视图上显示数据.my sql查询包含一些员工详细信息。我需要用他们的经理名称显示员工详细信息。



但是当我使用我的var类型的结果时我获得运行时异常的列表





我的代码在控制器动作中





var emp =来自m in t。员工

加入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};



返回查看(emp);





并且在视图中

@model IEnumerable< ; mvc4application.models.employee>



并收到错误







传入字典的模型项的类型为System.Data.Objects.ObjectQuery`1 [<> f__AnonymousType1`6 [System.Int32,System.String,

System.String,System.String ,System.Nullable`1 [System.Int32],System.String]]',但是这个字典需要一个类型为'System.Collections.Generic.IEnumerable`1 [Mvc4application.Models.Employee]'的模型项。 />
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value)+405487





关闭课程i我理解这一点是因为我的观点是使用Mvc4application.Models.Employee类型。



任何解决方案都很受欢迎。我无法将其转换为模型类型。

Hi
Please Help


I am using LINQ Self Join Query to display data on the view .my sql query contains some employees details .i need to show employee details with their Manager Name .

but when i use my var type of resultant list of i get runtime exception


my code is in controller action


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);


and in view
@model IEnumerable <mvc4application.models.employee>

and getting error



The model item passed into the dictionary is of type System.Data.Objects.ObjectQuery`1[<>f__AnonymousType1`6[System.Int32,System.String,
System.String,System.String,System.Nullable`1[System.Int32],System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Mvc4application.Models.Employee]'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +405487


Off Course i am understanding this because my view is using Mvc4application.Models.Employee type .

any solution is appreciated .as i am not able to convert it to model type .

推荐答案

你需要具体化查询,

you need to materialize the query,
return View(emp.ToList());


这篇关于ASP .NET MVC SELF加入视图查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆