我正在研究一个模拟MVC项目。我使用Entity framework 5.0创建了一个名为" EmployeeDataModel"的实体数据模型。知道所有课程 该实体是AutoGenerated(并且无法更改),我想改变关于模型的一些小事情,我决定创建一个" EditModel"文件夹,我猜这是什么" EditModel"是为了。然后我在该文件夹中创建了一个Parent类,我在自动生成的Employee.cs类中复制了很多东西。然后我创建了一个控制器类,它具有完全相同的EmployeeController的Index方法代码和完全相同的视图,除了这将被硬编码为使用Parent Model类而不是MVCDemo.Models.Employee。
然而,当我尝试运行该页面时,我收到此错误;
(传入字典的模型项类型为' System.Collections.Generic.List 1[MVCDemo.Models.Employee]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [MVCDemo.EditModels.Parent]')
这意味着我的视图期望一个对象列表,但它正在接收一个对象。 如果我删除" IEnumerable"我的观点的一部分,让它成为" @model MVCDemo.EditModels.Parent"它会传递这个错误,但是我等待列表循环的事实仍然存在!在我的控制器中,我将列表发送回View"返回View(employees.ToList());"
如果我选择了内置模型的员工模型,那么当我创建视图时,页面将运行没有任何问题。我在Employee.cs类中找不到任何指示,如果它返回一个列表!
请帮助我理解这一点。
这是我的Employee.cs(自动生成的类)
namespace MVCDemo.Models
{
using System;
using System.Collections.Generic;
public partial class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public int DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
}
这是我的索引视图;
@model IEnumerable<MVCDemo.Models.Employee>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.Department.DepName )
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department.DepName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.EmployeeId }) |
@Html.ActionLink("Details", "Details", new { id=item.EmployeeId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.EmployeeId })
</td>
</tr>
}
</table>
我在EditModel文件夹中的Parent类;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCDemo.Models;
namespace MVCDemo.EditModels
{
public class Parent
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public int DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
}
EmployeeController;
namespace MVCDemo.Controllers
{
public class EmployeeController : Controller
{
private EmployeeContext db = new EmployeeContext();
//
// GET: /Employee/
public ActionResult Index()
{
var employees = db.Employees.Include(e => e.Department);
return View(employees.ToList());
}
public ActionResult EmployeeParent()
{
var employees = db.Employees.Include(e => e.Department);
return View(employees.ToList());
}
和Employeeparent View;
@model IEnumerable<MVCDemo.EditModels.Parent>
@{
ViewBag.Title = "EmployeeParent";
}
<h2>EmployeeParent</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.EmployeeId)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.DepartmentId)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.DepartmentId)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
答案 0 :(得分:1)
此查询返回List<MVCDemo.Models.Employee>
,但视图正在等待IEnumerable<MVCDemo.EditModels.Parent>
...
var employees = db.Employees.Include(e => e.Department);
有两种方法可以解决这个问题......
1)更改视图预期的模型类型...
@model IEnumerable<MVCDemo.Models.Employee>
2)或者更改查询以返回MVCDemo.EditModels.Parent
s ...
using MVCDemo.EditModels;
// ...
public ActionResult EmployeeParent()
{
var parents = db.Employees.Include(e => e.Department)
.Select(e => new Parent
{
EmployeeId = e.EmployeeId,
Name = e.Name,
Gender = e.Gender,
City = e.City,
DepartmentId = e.DepartmentId,
Department = e.Department
});
return View(parents.ToList());
}
如果您的目标是对由Entity Framework生成的生成的Employee
类进行更改,则EF实际上会将模型生成为部分类,因此您可以通过创建部分类来添加它,并赢得它模型重新生成时不会被覆盖。例如......
public partial class Employee
{
public string SomeOtherProperty { get; set; }
}