MVC错误 - 传递到字典的模型项的类型是“System.Collections.Generic.List` [英] MVC error - The model item passed into the dictionary is of type 'System.Collections.Generic.List`
问题描述
我也弄不清是怎么回事这个错误:
I can't figure out what's going on with this error:
传递到字典的模型项的类型是System.Collections.Generic.List1 [RepositoryExample.Employee]',但本词典需要类型的模型项目RepositoryExample.Models.IEmployeeManagerRepository
我得到的错误,当我去到索引视图。我加了索引视图从控制器,但在它里面没有code。我使用LINQ to SQL的。
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>
这是我的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();
}
}
任何想法,我做错了什么?我想跟随本教程库模式的例子(http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs)
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)
推荐答案
在你的 Index.cshtml
视图顶部的替换:
In the top of your Index.cshtml
view replace:
@model RepositoryExample.Models.IEmployeeManagerRepository
与
@model IEnumerable<RepositoryExample.Employee>
的 _repository.ListEmployees()
方法的返回值的IEnumerable&LT;员工&GT;
,这就是要传递什么样的看法这里:
The _repository.ListEmployees()
method returns IEnumerable<Employee>
and that's what you are passing to the view here:
return View(_repository.ListEmployees());
这就是你应该在你的视图中使用了 @model
指令类型。
这篇关于MVC错误 - 传递到字典的模型项的类型是“System.Collections.Generic.List`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!