Retrieve Ienumerable values from View

Asked

Viewed 430 times

1

I have a table/model called Itemtabelapreco:

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; }
}

And another call Tabelapreco:

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

In the table view I load the list:

@model Aplicativo.Models.TabelaPreco

@foreach (var list in Model.ListaItemTabelaPreco)
{
    @Html.Raw(list.Nome)
    @Html.TextBoxFor(Model => list.ValorUnitario)
    @Html.TextBoxFor(Model => list.QtdPacote)
    @Html.TextBoxFor(Model => list.ValorPacote)
}

The problem I have is time to recover the values of this view list in the edition of the Table in the controller after the post, I have tried several ways and always comes the empty list, the other fields that are not from this list, comes the values correctly:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(TabelaPreco tabelaPrecoModel)
{
    tabelaPrecoModel.ListaItemTabelaPreco VEM SEMPRE NULL.
}

If anyone can help, I’d appreciate it.

  • I think I have a solution, I just need some information to be sure. Your view is to EDIT the Checklist or for you to ADD new items to it?

  • Good morning @Fabridamazio, really forgot to report this, fill the class property TabelaPreco no Get da view Edit: IEnumerable<ItemTabelaPreco> ListaItemTabelaPreco In the Edit view I run the foreach to display the list of items from ListaItemTabelaPreco by clicking on the Save Post button through the ActionResult Edit, however, this list is always empty.

  • You know how to use Viewmodels?

  • @Fabridamazio I have used, but I do not know if in a correct way, but if you can clarify a little the correct form of use, I believe I can use yes.

Show 1 more comment

1 answer

-1

Try changing the foeach by a traditional for

  @for (int i = 0; i != Model.ListaItemTabelaPreco.Count(); ++i)
 {
@Html.Raw(list.Nome)
@Html.TextBoxFor(m => Model.ListaItemTabelaPreco[i].ValorUnitario)
@Html.TextBoxFor(m=> Model.ListaItemTabelaPreco[i].QtdPacote)
@Html.TextBoxFor(m => Model.ListaItemTabelaPreco[i].ValorPacote)
 }
  • In this case there is no difference between the foreach and the for, your answer makes no sense.

  • There is no parameter index to identify the line, and the Modelbinder does not predict the cardinality N. Furthermore, this solution does not guarantee the reassembly of the form in case of validation failure, for example.

Browser other questions tagged

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