It seems like a lot of people have this issue, but I haven't found a solution to my specific case yet.
I start by sending my tickets to my view
[HttpGet]
public ActionResult AddPopcorn()
{
List<Ticket> tickets = new List<Ticket>();
// Fill the list with tickets
return View("AddPopcorn", tickets);
}
In the view I display the tickets with a form and checkbox. The view displays the tickets correctly.
@model List<Domain.Entities.Ticket>
@{
ViewBag.Title = "AddPopcorn";
}
<body>
// Html stuff
@using (Html.BeginForm("AddPopcorn", "Ticket", FormMethod.Post))
{
<table>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TicketID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.TicketType)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.Popcorn)
</td>
</tr>
}
</table>
<input type="submit" value="Voeg toe!" />
}
</div>
</body>
If people check the checkbox, I want the value 'Popcorn' to be true. So I want the view to return a list of tickets with updated values for popcorn based on the checkbox.
[HttpPost]
public ActionResult AddPopcorn(List<Ticket> model)
{
foreach (var item in model)
{
if (item.Popcorn == true)
{
item.Price = item.Price + 5;
}
}
return RedirectToAction("Payment", "Payment");
}
However, the model returned to AddPopcorn is null. I can't seem to figure out why.