【发布时间】:2019-01-23 17:05:38
【问题描述】:
我在安装和显示此查询时遇到错误。
var itens = _context.ContasReceber
.Include(x => x.Pessoas)
.Include(x => x.PlanosServicos)
.Select(c => new
{
Identificador = c.Pessoas.NIdentificador,
NomePessoa = c.Pessoas.Nome,
c.Observacao,
c.Vencimento,
c.Valor,
c.Quitado,
c.DataPagamento,
c.ValorPago
})
.ToList();
ViewData["Contas"] = itens;
cshtml:
@foreach (var item in ViewBag.Contas)
{
<tr>
<th>
@item.Identificador
</th>
<th>
@item.NomePessoa
</th>
<th>
@item.Observacao
</th>
<th>
@item.Vencimento
</th>
<th>
@item.Quitado
</th>
<th>
@item.DataPagamento
</th>
<th>
@item.ValorPago
</th>
</tr>
}
错误是:
处理请求时发生未处理的异常。 RuntimeBinderException:“object”不包含“Identificador”的定义 CallSite.Target(Closure , CallSite , object )
【问题讨论】:
-
您遇到了this 问题。一个常见的解决方案是创建一个“ViewModel”,它具有您要在该特定视图中使用的属性,并将您的实体属性投影到其中。 AutoMapper 非常适合这个。
标签: asp.net entity-framework asp.net-core