我尝试将以下列表返回到我的视图中:
var Things = from a in db.Things
where a.ID == id
join b in db.IssueTypes on a.ID equals b.ID
select a;
List<Things> currentThings = Things.ToList();
ViewBag.currentThing = currentThings;
观点:
@foreach (var item in ViewBag.currentThing)
{
<table>
<tr>
<td>Title</td>
<td>@item.Title</td>
</tr>
<tr>
<td>ID</td>
<td>@item.ID</td>
</tr>
<tr>
<td>Description from Inner join:</td>
<td>@item.b.Description</td>
</tr>
</table>
}
我尝试将内部联接中的行显示为@item.b.Description
。但这不起作用。这样做的正确方法是什么?
答案 0 :(得分:0)
问题是,您只是预测a
,这意味着您的查询只返回db.Things
的属性,以获取db.IssueTypes
的属性,您必须同时投影此: -
var Things = from a in db.Things
where a.ID == id
join b in db.IssueTypes on a.ID equals b.ID
select new { a, b };
然后,将此直接指定给ViewBag
,如下所示: -
ViewBag.currentThing = Things;
最后,您可以在视图中使用b.Description
。
虽然,我建议您改为创建ViewModel
并创建强类型视图。