Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 .
Posted
Updated 11-Jul-13 3:31am
v2
Comments
Mukesh Ghosh 11-Jul-13 9:32am    
What you have added in your aspx page header?
gauravupadhyay 11-Jul-13 9:36am    
I am creating a MVC app and using RAZOR View engine .i am not using aspx.

1 solution

you need to materialize the query,
C#
return View(emp.ToList());
 
Share this answer
 
Comments
gauravupadhyay 12-Jul-13 0:45am    
I got this after using ---return View(emp.ToList());

[InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`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]'.]


return View(emp.ToList());


Is there any way to use return list to use as model in ASP.NET MVC View
Yuriy Loginov 12-Jul-13 8:09am    
I think the problem may be that new { Id = m.EmployeeID , Name = m.Name, Manager = e1.Name , Designation = m.Designation,Phone =m.Phone ,address = m.Address }; does not match the Mvc4application.Models.Employee definition. One way to check is to do this return View(emp.ToList<<mvc4application.models.employee>>());
If you get an exception when you run that it means you should check to make sure that the fields you have in your model are the same fields that you are declaring inside the anonymous type that your are using. This means that the names and the types of fields should match up exactly to your model

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900