Guto Barroso
Guto Barroso

Reputation: 131

Populating viewmodel array

What Am I doing wrong? In the code below (specifically on foreach), When I have more than one 'itens', just the last one will be on Itens array.

I am using a viewmodel array, as you can see below.

I would appreciate if you could give me a sample.

I created a repository class like this:

public async Task<PrintNFePedidoRoot> PedidoPrint(int idPedido)
{
    using var db = new KeplerContext(_optionsBuilder);

    var pedido = await db.Pedido.AsNoTracking()
        .FirstOrDefaultAsync(m => m.IdPedido == idPedido);

    var pedidoItens = await db.PedidoItens
        .Where(i => i.IdPedido == idPedido)
        .Include(p => p.IdProdutoNavigation)
        .AsNoTracking().ToListAsync();

    var pedidoPrint = new PrintNFePedidoRoot()
    {
        IdPedido = idPedido,
        TotalValor = pedido.Val,
        Desconto = 0,
        Taxa = pedido.Tax,
        TotalPagar = pedido.Tot,
    };

    var Itens = Array.Empty<PrintNFePedidoItens>();
   
    foreach (PedidoItens i in pedidoItens)
    {
        if (i.IdProdutoMeia != null)
        {
            var produto = db.Produto
                .AsNoTracking()
                .First(p => p.IdProduto == i.IdProdutoMeia);

            i.IdProdutoNavigation.Nome = i.IdProdutoNavigation.Nome + "/" + produto.Nome.Replace("Exp", "").Trim();
        }

        Itens = new[]
        {
            new PrintNFePedidoItens
            {
                IdProduto = i.IdProduto,
                Nome = i.IdProdutoNavigation.Nome,
                Valor = i.IdProdutoNavigation.Preco
            }
        };
    }

    pedidoPrint.Itens = Itens;

    return pedidoPrint;
}

I created a viewModel class like these:

    public class PrintNFePedidoRoot
    {
        [JsonProperty("id_pedido")]
        public int IdPedido { get; set; }

        [JsonProperty("total_valor")]
        public int TotalValor { get; set; }

        [JsonProperty("desconto")]
        public int Desconto { get; set; }

        [JsonProperty("taxa")]
        public int Taxa { get; set; }

        [JsonProperty("total_pagar")]
        public int TotalPagar { get; set; }

        [JsonProperty("pedido_itens")]
        public PrintNFePedidoItens[] Itens { get; set; }
    }

    public class PrintNFePedidoItens
    {
        [JsonProperty("id_produto")]
        public int IdProduto { get; set; }

        [JsonProperty("nome")]
        public string Nome { get; set; }

        [JsonProperty("valor")]
        public decimal Valor { get; set; }
    }

Upvotes: 0

Views: 259

Answers (2)

Guto Barroso
Guto Barroso

Reputation: 131

This way is working.

Instead of this:

    var Itens = Array.Empty<PrintNFePedidoItens>();

I have changed to this:

    var Itens = new List<PrintNFePedidoItens>();

Instead of this:

    public class PrintNFePedidoRoot
    {
       ...

        [JsonProperty("pedido_itens")]
        public PrintNFePedidoItens[] Itens { get; set; }
    }

I have changed to this:

    public class PrintNFePedidoRoot
    {
      ...

        [JsonProperty("pedido_itens")]
        public List<PrintNFePedidoItens> Itens { get; set; }
    }

So, I could used this:

   Itens.Add(new PrintNFePedidoItens
   {
         IdProduto = i.IdProduto,
         Nome = i.IdProdutoNavigation.Nome,
         Valor = i.IdProdutoNavigation.Preco,
         Qntd = i.Quantidade
   });

Upvotes: 0

Lvcios
Lvcios

Reputation: 49

Looks like you are assigning a new array with just one element to "Itens" instead to add values to the array.

Instead of this

Itens = new[]
    {
        new PrintNFePedidoItens
        {
            IdProduto = i.IdProduto,
            Nome = i.IdProdutoNavigation.Nome,
            Valor = i.IdProdutoNavigation.Preco
        }
    };

Try

Itens.Add(new PrintNFePedidoItens
        {
            IdProduto = i.IdProduto,
            Nome = i.IdProdutoNavigation.Nome,
            Valor = i.IdProdutoNavigation.Preco
        });

Just be sure you are initializing correctly "Itens".

Upvotes: 1

Related Questions