wyślij ViewModel, który zawiera listę z Html.BeginForm (MVC 4)
Mój viewmodel zawiera listę całkowitą, mam problem z tym, że gdy wysyłam zmodyfikowany model widoku formularza, zawsze jest on równy null.
Mój ViewModel:public class testViewModel
{
public List<int> itemTest { get; set;
}
Działanie w moim kontrolerze:Na przykład spróbuję zsumować nowe wartości wprowadzone do formularza, ale obliczona suma jest zawsze równa 0, nic się nie zmienia.
public ActionResult form(int nbre)
{
testViewModel montest = new testViewModel()
{
itemTest = new List<int>()
};
for(int i=0;i<nbre ;i++)
{
montest.itemTest.Add(0);
}
return View(montest);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult form(testViewModel maListe)
{
int somme = 0;
if (maListe.itemTest != null)
{
if (maListe.itemTest.Count() != 0)
{
foreach (var item in maListe.itemTest)
{
somme += item;
}
}
}
//listtest = maListe;
return RedirectToAction("test2", new { qte = somme });
}
Mój widok @model MvcWebRole1.ViewModels.testViewModel
@{
ViewBag.Title = "Formulaire";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table>
@foreach (var item in Model.itemTest)
{
<tr >
<td >
@Html.Label("Quantitée")
</td>
<td>
@Html.EditorFor(model => item)
@Html.ValidationMessageFor(model => item)
</td>
</tr>
}
</table>
<input type="submit" value="Valider" />
}
Dziękuję uprzejmie pomóż mi