Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
IEnumerable<int> employeeIds = Employee.GetAllEmployees()
    .Select(emp => emp.EmployeeID);
foreach (int id in employeeIds)
{
    Console.WriteLine(id);
}




C#
public static List<Employee> GetAllEmployees()
       {
          EmployeeDBContext db = new EmployeeDBContext();
           List<Employee> lstEmployees = new List<Employee>();

           lstEmployees = db.Employees.ToList();
           return lstEmployees;

       }



Output:
101
102
103
------


I am getting Console Output properly, but I want to show same output in Asp.Net MVC : View.

What I have tried:

C#
public ActionResult EmpOutput()
       {
           StudentDBContext db = new StudentDBContext();
           IEnumerable<int> employeeIds = db.Employees
           .Select(emp => emp.Id);
           return View(employeeIds);
       }




Error :
C#
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.Int32]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[DemoWithMVC.Models.Employee]'.



Please Help me where I going wrong. Thank You in Advanced.
Posted
Updated 16-Dec-16 0:03am
Comments
Aman.Jen 30-Dec-16 6:07am    
Thank You So Much

1 solution

Your view is expecting a list of Employee objects and you are passing a list of ints. Either change the view to accept a list of ints or change your code to pass a list of employees. We don't know which solution you need as we can't see your view.

C#
return View(db.Employees.ToList());
 
Share this answer
 

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