I create a table in my CSHTML, I want to pass the array of nr of items == aantal back to my controller however this doesn't seem to work. Any idea whats wrong or why I get a null reference in my controller?
CSHTML
@using (Html.BeginForm("OrderConfirm", "Beurs", new { vm = Model.Aantal }, method: FormMethod.Post))
{
<table class="table table-striped table-condensed table-bordered">
<tr>
<th>
Naam
</th>
<th>
Prijs
</th>
<th>
Minimum prijs
</th>
<th>
Factor
</th>
<th> Actie</th>
<!--
<th>Edit</th>-->
</tr>
@foreach (var item in Model.ItemLijstVm)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Naam)
</td>
<td>
€ @Html.DisplayFor(modelItem => item.Prijs)
</td>
<td>
€ @Html.DisplayFor(modelItem => item.MinimumPrijs)
</td>
<td>
@Html.DisplayFor(modelItem => item.Factor)
</td>
<td>
@Html.TextBoxFor(m => m.Aantal[item.Id - 1], new {type = "number" })
</td>
</tr>
}
</table>
<input type="submit" value=" Bevestig bestelling " width="120" />
}
ViewModel
public class BeursLijstViewModel
{
public IEnumerable<BeursItemViewModel> ItemLijstVm{get; set;}
public string Naam { get; set; }
public double Crash { get; set; }
//References naar animated gif
public bool Event { get; set; }
public string GifPath { get; set; }
public int[] Aantal { get; set; }
public int VerhoogAllePrijzen { get; set; }
public double Totaal { get; set; }
public SelectListItem Categorie { get; set; }
public BeursLijstViewModel(Beurs beurs)
{
ItemLijstVm= beurs.Items.Select(g => new BeursItemViewModel(g));
Naam = beurs.Naam;
Aantal = new int[beurs.Items.Count()];
Totaal = beurs.Totaal;
}
}
Controller
[HttpPost]
public ActionResult OrderConfirm(int[] vm) //VM is null but should be array
{
//Some more code
}
The reference I get on my post from my model is null, but if i declare it in my foreach loop like this, it works. I really don't have a clue what goes wrong:
@using (Html.BeginForm("Add", "Beurs", new { id = item.Id, aantal = Model.Aantal }, method: FormMethod.Post))
{
@Html.TextBoxFor(m => m.Aantal[item.Id - 1], new { type = "number" })
<input type="submit" value=" + " width="120"/>
}