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?
– Fabri Damazio
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 theforeach
to display the list of items fromListaItemTabelaPreco
by clicking on the Save Post button through theActionResult Edit
, however, this list is always empty.– Max
You know how to use Viewmodels?
– Fabri Damazio
@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.
– Max
A classic 1:N cardinality problem, solvable by using Begincollectionitem.
– Leonel Sanches da Silva
If you need a tutorial, use this answer.
– Leonel Sanches da Silva