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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – MVC错误 – 传入字典的模型项目的类型为“System.Collections.Generic.List”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不知道这个错误发生了什么: @H_404_2@传入字典的模型项目的类型为“System.Collections.Generic.List1 [RepositoryExample.Employee]”,但是该字典需要一个类型为“RepositoryExample.Models.IEmployeeManagerRepository”的模型项.

@H_404_2@当我转到索引视图时,我收到错误.我从控制器添加了索引视图,但没有代码.我使用Linqsql.

@model RepositoryExample.Models.IEmployeeManagerRepository

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>
@H_404_2@这是我的代码

@H_404_2@EmployeeController.cs

// GET: /Employee/
    public ActionResult Index()
    {
        return View(_repository.ListEmployees());
    }
@H_404_2@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(); 

    }

}
@H_404_2@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(); 
}
@H_404_2@}

@H_404_2@任何想法我做错了什么?我试图按照本教程中的Repository模式的例子(http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs)

解决方法

在您的Index.cshtml视图的顶部替换:
@model RepositoryExample.Models.IEmployeeManagerRepository
@H_404_2@有:

@model IEnumerable<RepositoryExample.Employee>
@H_404_2@_repository.ListEmployees()方法返回IEnumerable< Employee>这就是你在这里的观点:

return View(_repository.ListEmployees());
@H_404_2@这就是您在视图中的@model指令中应该使用的类型.

猜你在找的asp.Net相关文章