2
I have a viewmodel in my project. Within it I have two entities that are a list of items.
I’m doing the data editing logic of these entities that are inside the viewmodel. I have 5 entities in all. Those that are not a list, I can popular the data in the relevant fields in view, now, the list ones I can’t. I can load the object and fill it, but I can’t pass this 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 the 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>
Putting the
<input type="hidden" name="RefRefeicao.index" id="RefRefeicao_index" value="@i.ToString()" />didn’t work. =(– Érik Thiago
What didn’t work? Tag generation? Form submission? Filling out? See this answer, where it is done manual.
– Leonel Sanches da Silva
Filling in the data... The fields remain blank.. The data is sent, but is still blank. =(
– Érik Thiago
Ah, good. Naturally they will go blank, because their approach, using
@Html.TextBox, does not define values (the value is the second parameter, in which you are passingnullfor everyone). By trial and error will not work even.– Leonel Sanches da Silva
How could I solve this situation? Putting the inputs in pure html instead of using Razor? Or da para resolver using Razor?
– Érik Thiago
Well, if I mentioned
null, that it represents the value, replacing thenullby the value will naturally appear theinputfilled.– Leonel Sanches da Silva
But like, being a list, how could I replace that value?
– Érik Thiago
Element by element, young. Inside the
foreach, exactly as you were doing to write each other’s nameinput.– Leonel Sanches da Silva
I tried and it didn’t work.. I put as string:
@Html.TextBox(string.Format("RefRefeicao[{0}].RefTipo", i), string.Format("RefRefeicao[{0}].RefTipo", i), new { @class = "form-control" })and it doesn’t work.. = (.. What is shown is Refresh[{n}]. Type– Érik Thiago