我试图加入两个表并在强类型列表视图中显示结果,其中包含来自两个表的列,
public ActionResult OrderDetails(int id) {
var query = from o in db.OrderDetails
join a in db.Albums on o.AlbumId equals a.AlbumId
where o.OrderId == id
select new OrderDetail() {
OrderId = o.OrderId,
AlbumId = o.AlbumId,
Quantity = o.Quantity,
UnitPrice = o.UnitPrice,
Album = new Album {
Title = a.Title
}
};
return View(query);
}
这是我的看法
@model IEnumerable<QWERK.OrderDetail>
@{
ViewBag.Title = "OrderDetails";
}
<h2>OrderDetails</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.OrderId)
</th>
<th>
@Html.DisplayNameFor(model => model.AlbumId)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.UnitPrice)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.OrderId)
</td>
<td>
@Html.DisplayFor(modelItem => item.AlbumId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.Album.Title)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.OrderDetailId }) |
@Html.ActionLink("Details", "Details", new { id=item.OrderDetailId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.OrderDetailId })
</td>
</tr>
}
</table>
但我不断收到 NotSupportedException
我很确定我得到了错误,因为我使用的是 OrderDetails 模型,但是我怎样才能解决这个问题来显示来自不同表的多个列????任何帮助将不胜感激...谢谢