【发布时间】:2011-10-02 03:49:04
【问题描述】:
我正在尝试在视图中显示列表,如何解决此错误?
传入字典的模型项是类型 'System.Data.Objects.ObjectQuery
1[System.Linq.IGrouping2[System.Int32,mvc3Post.Models.Contact]]', 但是这本字典需要一个类型的模型项 'System.Collections.Generic.IEnumerable`1[mvc3Post.Models.Contact]'。
控制器:
public ActionResult Index()
{
AdventureWorksEntities db = new AdventureWorksEntities();
var result = from soh in db.SalesOrderHeaders
join co in db.Contacts
on soh.ContactID equals co.ContactID
orderby co.FirstName
group co by co.ContactID into g
select g;
return View(result.AsEnumerable());
}
查看:
@model IEnumerable<mvc3Post.Models.Contact>
@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
NameStyle
</th>
<th>
Title
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.NameStyle)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ContactID }) |
@Html.ActionLink("Details", "Details", new { id = item.ContactID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ContactID })
</td>
</tr>
}
</table>
【问题讨论】:
标签: linq asp.net-mvc-3 c#-4.0