I have a viewmodel in my project. Inside it I have two entities which are a list of items.
I'm doing the data-editing logic for these entities that are within the viewmodel . I have 5 entities in all. The ones that are not a list, I get the data in the fields in the view , now those that are list I can not. I can load the object and fill it, but I can not pass that data to the view .
The codes I have are:
public ActionResult Edit(int? id)
{
CliCliente cliente = db.CliCliente.Find(id);
AnaAnamineseAlimentar anamnese = db.AnaAnamineseAlimentar.Find(id);
RecRecordatorio recordatorio = db.RecRecordatorio.Find(id);
List<RefRefeicao> refeicao = anamnese.RefRefeicao; //db.RefRefeicao.Find(id);
List<QfaQuestionarioFrequenciaAlimentar> qfa = anamnese.QfaQuestionarioFrequenciaAlimentar;//= db.QfaQuestionarioFrequenciaAlimentar.Find(id);
for (int i = 0; i < qfa.Count; i++)
{
qfa[i].AnaId = anamnese.AnaId;
qfa[i].AnaAnamineseAlimentar = anamnese;
}
for (int i = 0; i < refeicao.Count; i++)
{
refeicao[i].AnaId = anamnese.AnaId;
refeicao[i].AnaAnamineseAlimentar = anamnese;
}
AnamineseViewModel viewModel = new AnamineseViewModel()
{
CliCliente = cliente,
AnaAnamineseAlimentar = anamnese,
RecRecordatorio = recordatorio,
RefRefeicao = refeicao,
QfaQuestionarioFrequenciaAlimentar = qfa
};
return View(viewModel);
}
In view :
@model NutriSport.Models.AnamineseViewModel
<div class="form-horizontal">
<br />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<table class="table">
<tr class="success">
<th>
Tipo
</th>
<th>
Horário/Local
</th>
<th>
Alimentos/Quantidades
</th>
</tr>
@for (int i = 0; i <= Model.RefRefeicao.Count; i++)
{
<tr class="success">
<td>
@Html.TextBox(string.Format("RefRefeicao[{0}].RefTipo", i), null, new { @class = "form-control" })
</td>
<td>
@Html.TextBox(string.Format("RefRefeicao[{0}].RefHorarioLocal", i), null, new { @class = "form-control" })
</td>
<td>
@Html.TextArea(string.Format("RefRefeicao[{0}].RefAlimentosQuantidades", i), null, new { @class = "form-control" })
</td>
</tr>
}
</table>
</div>