asp.net-mvc MVC 错误 - 传入字典的模型项的类型为“System.Collections.Generic.List”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7573020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:29:46  来源:igfitidea点击:

MVC error - The model item passed into the dictionary is of type 'System.Collections.Generic.List`

asp.net-mvcasp.net-mvc-3razorienumerable

提问by MikeB55

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'.`

传递到字典中的模型项的类型为“System.Collections.Generic.List1[RepositoryExample.Employee]”,但该字典需要类型为“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.

转到“索引”视图时出现错误。我从控制器添加了索引视图,但其中没有代码。我正在使用 Linq to SQL。

@model RepositoryExample.Models.IEmployeeManagerRepository

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

This is my code:

这是我的代码:

EmployeeController.cs

员工控制器.cs

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

LinqEmployeeManagerRepository.cs

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

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.

任何想法我做错了什么?我正在尝试遵循本教程中存储库模式的示例:http: //www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs

回答by Darin Dimitrov

In the top of your Index.cshtmlview replace:

在您的Index.cshtml视图顶部替换:

@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:

_repository.ListEmployees()方法返回IEnumerable<Employee>,这就是您传递给此处视图的内容:

return View(_repository.ListEmployees());

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

所以这就是您应该@model在视图中的指令中使用的类型。