0
I have a query that returns a list with objects. From this list, I go through each position, define some object parameters and return to a view.
What I’m trying to do is:
Scroll through all positions on this list and generate a single view. However, what happens is that when I run the method, my view only loads the first object from the list.
public async Task<IHttpActionResult> Get(string cnpj, int numero, string chave)
{
var notaDb = await DbContext.GetNotaAsync(cnpj, numero, chave);
var notas = DbContext.ObterTodasNotas(notaDb.Prestador.Id);
var items = new List<NotaFiscalViewModel>();
if (notaDb == null)
{
return NotFound();
}
foreach (var nota_ in notas)
{
var nota = await GetNotaAsync(nota_);
var cidade = await DbContext.GetMunicipioNomeAsync(notaDb.CodigoTributacaoMunicipio);
nota.CodigoTributacaoMunicipio = cidade.Descricao + " - " + cidade.Estado.UF;
var prefeitura = await DbContext.GetPrefeituraAsync();
nota.Imagem = prefeitura.Imagem;
nota.Prefeitura = await ContribuinteController.GetContribuinteViewModelAsync(prefeitura);
var cnpjPrefeitura = await DbContext.GetConfiguracaoAsync();
nota.Prefeitura.CpfCnpj = cnpjPrefeitura.CnpjPrefeitura;
return Ok<NotaFiscalViewModel>(nota);
}
return Ok();
}
Opa, I came to fix this. However, it only returns the last item in the list.
– Vinícius
And for you to pass 'note' out of the foreach, you need to declare it first. That way your method will never compile.
– Vinícius
@Viniciusmatos true, I did not pay attention to it; you want to return the list of notes?
– Barbetta