【发布时间】:2013-06-17 16:58:37
【问题描述】:
我正在 Entity Framework 中进行左外连接,并将输出发送到 ViewBag 以显示在表格中。但是,View 中不显示任何内容。我的代码如下。
var clients = db.Client
.Include("ClientInformation")
.Where(t => t.IsApplicationRegistered == false)
.Take(20);
var q = (from c in clients
join a in db.Agent
on c.AgentUserId equals a.AgentUserId into jointable
from x in jointable.DefaultIfEmpty()
select new
{
c.ClientId,
c.ClientInformation.FirstName,
c.ClientInformation.FamilyName,
c.ClientInformation.Address,
AgentFirstName = x.FirstName,
AgentLastName = x.LastName
}).ToList();
ViewBag.clients = q;
这会检索结果(我可以在调试期间通过 IntelliSense 看到它们)但视图不显示任何内容。示例视图如下。
@foreach (dynamic item in ViewBag.clients)
{
<tr>
<td>@item.FirstName @item.FamilyName</td>
<td>@item.Address</td>
<td>@item.AgentFirstName @item.AgentLastName</td>
</tr>
}
有什么想法吗?谢谢。
更新:
在View 里面我得到一个异常
'object' does not contain a definition for 'FirstName'
但是,当我将鼠标悬停在 foreach 循环内的 item 上时,我可以清楚地看到它在那里。
【问题讨论】:
标签: asp.net-mvc entity-framework ef-code-first