【发布时间】:2011-08-05 12:43:13
【问题描述】:
我的项目不断收到此错误。
传入字典的模型项是类型 'System.Collections.Generic.List
1[<>f__AnonymousType22[System.String,System.String]]', 但是这本词典需要一个类型的模型项 'System.Collections.Generic.IEnumerable`1[ETMS.Models.DB.tblParent]'。描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。
================================================ ====================================
我要做的是从数据库中检索数据(一对多),这是我的数据库表结构
tblparent
- Parent_ID
- Username
- Password
- Firstname
- Lastname
和
tblParentEmail
- ParentEmail_ID
- Email
- Parent_ID
我是从电子邮件到父母的外来关系,但是当出现另一个错误时,我无法将其包含在 EF 中。我这样做并导致我出现此错误:
public ActionResult Clientlist()
{
using (ETMSPeopleEntities db = new ETMSPeopleEntities())
{
//var sxc = db.tblParents.Include("tblLocation").Include("tblParentEmails.ParentEmail_ID")
// .OrderByDescending(p => p.Status).ToList();
var members = (from x in db.tblParentEmails
join y in db.tblParents
on x.Parent_ID equals y.Parent_ID
select new { Email = x.ParentEmail, UserName = y.Username }).AsEnumerable();
return View(members.ToList());
}
}
这是我的管理员控制器
@model IEnumerable<ETMS.Models.DB.tblParent>
@{
ViewBag.Title = "Clientlist";
}
<h2>Clientlist</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Username
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Username)
</td>
<td>
@Html.DisplayFor(modelItem => item.Firstname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Lastname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreateTime)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Parent_ID }) |
@Html.ActionLink("Details", "Details", new { id=item.Parent_ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Parent_ID })
</td>
</tr>
}
</table>
这是客户列表视图
【问题讨论】:
-
还有什么错误?您的观点与您的查询有什么关系?您的视图的预期数据和查询的返回日期完全不同。
-
如果您希望您的视图显示来自
tblparent和tblParentEmail的项目,请创建一个具有视图所需的所有属性的 ViewModel 类,并将您的视图绑定到此类(而不是 @ 987654328@) 并在控制器中,而不是使用匿名类型 (select new {...),而是使用视图模型类。
标签: linq asp.net-mvc-3 entity-framework-4.1