查询显示错误 [英] Query Display Error

查看:81
本文介绍了查询显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在安装和显示此查询时出现错误。

I'm getting an error while mounting and displaying this query.

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:'对象'不包含'Identificador'的定义
CallSite.Target(Closure,CallSite,object)

An unhandled exception occurred while processing the request. RuntimeBinderException: 'object' does not contain a definition for 'Identificador' CallSite.Target(Closure , CallSite , object )

推荐答案

在剃须刀页面PageModel中,可以使用ViewModel返回自定义数据。
这是演示的简化版本。

In razor pages PageModel, you could use ViewModel to return custom data. Here is a simplified version of the demo.

在cshtml.cs中:

In cshtml.cs:

 public class ContasViewModel
    {
        public string Identificador { get; set; }
        public string NomePessoa { get; set; }
        public string Observacao { get; set; }
    }

    public IList<ContasViewModel> Contas { get; set; }

    public async Task OnGetAsync()
    {

        Contas = await _context.ContasReceber
          .Include(x => x.Pessoas)
          .Include(x => x.PlanosServicos)
          .Select(c => new ContasViewModel
          {
              Identificador = c.Pessoas.NIdentificador,
              NomePessoa = c.Pessoas.Nome,
              Observacao= c.Observacao

          }).ToListAsync();

    }

在cshtml中:

 @foreach (var item in Model.Contas)
    {
        <tr>
            <th>
                @item.Identificador
            </th>
            <th>
                @item.NomePessoa
            </th>
            <th>
                @item.Observacao
            </th>

        </tr>
    }

这篇关于查询显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆