12

I can't figure out what's going on with this error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[RepositoryExample.Employee]', but this dictionary requires a model item of type 'RepositoryExample.Models.IEmployeeManagerRepository'.`

I get the error when I go to the Index view. I added the Index View from the controller but there is no code in it. I'm using Linq to SQL.

@model RepositoryExample.Models.IEmployeeManagerRepository

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

This is my code:

EmployeeController.cs

    // GET: /Employee/
    public ActionResult Index()
    {
        return View(_repository.ListEmployees());
    }

LinqEmployeeManagerRepository.cs

public class LinqEmployeeManagerRepository: RepositoryExample.Models.IEmployeeManagerRepository
{
    private DeptDirectoryDataClassesDataContext _db = new DeptDirectoryDataClassesDataContext();

    public Employee GetEmployee(string UserName)
    {
        return (from e in _db.Employees where e.UserName == UserName select e).FirstOrDefault();
    }

    public IEnumerable<Employee> ListEmployees()
    {
        return _db.Employees.ToList();
    }

    public Employee CreateEmployee(Employee employeeToCreate)
    {
        _db.Employees.InsertOnSubmit(employeeToCreate);
        _db.SubmitChanges();
        return employeeToCreate; 
    }

    public Employee EditEmployee(Employee employeeToEdit)
    { 
        var OriginalEmployee = GetEmployee(employeeToEdit.UserName);
        _db.Employees.Attach(employeeToEdit, OriginalEmployee);
        _db.SubmitChanges();
        return employeeToEdit; 
    }

    public void DeleteEmployee(Employee employeeToDelete)
    {
        var OriginalEmployee = GetEmployee(employeeToDelete.UserName);
        _db.Employees.DeleteOnSubmit(OriginalEmployee);
        _db.SubmitChanges();     
    }
}

IEmployeeManagerRepository.cs

namespace RepositoryExample.Models
{
    public interface IEmployeeManagerRepository
    {
        Employee CreateEmployee(Employee employeeToCreate);
        void DeleteEmployee(Employee employeeToDelete);
        Employee EditEmployee(Employee employeeToUpdate);
        Employee GetEmployee(string UserName);
        IEnumerable<Employee> ListEmployees(); 
    }
}

Any ideas what I'm doing wrong? I'm trying to follow the example on Repository pattern in this tutorial: http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
MikeB55
  • 291
  • 1
  • 4
  • 18

1 Answers1

19

In the top of your Index.cshtml view replace:

@model RepositoryExample.Models.IEmployeeManagerRepository

with:

@model IEnumerable<RepositoryExample.Employee>

The _repository.ListEmployees() method returns IEnumerable<Employee> and that's what you are passing to the view here:

return View(_repository.ListEmployees());

So that's the type you should be using in the @model directive in your view.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Ok, i did that but now i get a new error: Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0234: The type or namespace name 'Employee' does not exist in the namespace 'RepositoryExample.Models' (are you missing an assembly reference?) – MikeB55 Sep 27 '11 at 17:13
  • @MikeB55, OK, use the correct namespace where this `Employee` class is defined. `@model IEnumerable`. I have updated my answer. – Darin Dimitrov Sep 27 '11 at 17:15
  • Excellent. Thank you so much. I've been searching for this error for the past 3 hours. I got it to work now by adding @model IEnumerable as you suggested. – MikeB55 Sep 27 '11 at 17:17
  • 1
    @MikeB55, if this post helped you solve the problem you were having you should consider [marking it as answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) by clicking on the tick next to it. – Darin Dimitrov Sep 27 '11 at 17:24
  • I just did. Tried before but system told me something about waiting 5 min. before I could do it. – MikeB55 Sep 27 '11 at 17:27