Recover Ienumerable Value in Post

Asked

Viewed 20 times

1

Good morning, I created a request for help on this before and it was marked as duplicate and some who tried to help me, but I think they misunderstood what I’m struggling with.

I have these two classes:

public class TabelaPreco
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public decimal Valor { get; set; }
    public bool Status { get; set; }
    public int TipoServicoId { get; set; }
    public IEnumerable<ItemTabelaPreco> ListaItemTabelaPreco { get; set;
}

public class ItemTabelaPreco
{
    public string Nome { get; set; }
    public decimal ValorUnitario { get; set; }
    public int QtdPacote { get; set; }
    public decimal ValorPacote { get; set; }
    public int TabelaPrecoId { get; set; }
}

Inside the class Table I have a list of items of type Itemtabelapreco.

I recover without problems the values of both the Tabelaprice and Itemtabelapreco database to display in the edit view, the Listitemtabelapreco comes filled without problems.

inserir a descrição da imagem aqui

My problem is that when I click on Save that gives the Post to the controller, the Listitemtabelapreco comes NULL always, the other fields come values correctly, I tried to use some examples that the staff indicated of partial and viewmodel, but it kept coming null.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(TabelaPreco tabelaPrecoModel)

inserir a descrição da imagem aqui

  • 1

    Hello @Max. Actually, I think you don’t understand the answers. What you want is to send a list of data in the POST, correct? So that’s what the other answers say. If you want other examples, all these answers speak of the same thing. You can do by for(), using the BeginCollectionItem or in any other way that I can find, but the problem seems the same to me. If you have any specific questions with the use of any of these forms, you can open a more specific question, which will be easier to help you.

  • @Randrade, really following one of the topics you indicated I was able to make work and it was what the people were saying anyway, I was not able to understand. But could you or someone else please explain to me why this way works and why the other one doesn’t? Thank you!

  • I’ll be happy to explain. But, to leave in the "collection" of the site, I ask you to open another question with this doubt, so it will be for all who have this same doubt

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.