Ok so there are lots of questions regarding this, but I can't seem to get this to work. It should be simple, but I'm left scratching my head.
Controller:
[HttpPost]
public ActionResult Edit(BookingIndexViewModel vm) // Also tried BookingViewModel
{
return View();
}
Container view model:
public class BookingIndexViewModel
{
public int id { get; set; }
public List<BookingViewModel> bookings { get; set; }
public List<SelectListItem> allAttendances { get; set; }
}
Booking view model (the VM I'm actually interested in)
public class BookingViewModel
{
public int id { get; set; }
public int referralId { get; set; }
public int attendanceId { get; set; }
}
The View
@model Project.ViewModels.BookingIndexViewModel
@using Project.ViewModels
<fieldset>
<legend>Registered patients</legend>
@if (Model.bookings.Count < 1)
{
<div>None currently registered</div>
}
else
{
<table>
<tr>
<th>
<span class="display-label">@Html.LabelFor(model => model.bookings[0].forenames)</span>
</th>
<th>
<span class="display-label">@Html.LabelFor(model => model.bookings[0].surname)</span>
</th>
<th>
<span class="display-label">@Html.LabelFor(model => model.bookings[0].dob)</span>
</th>
<th>
<span class="display-label">@Html.LabelFor(model => model.bookings[0].attendance</span>
</th>
</tr>
@{
foreach (BookingViewModel b in Model.bookings)
{
using (Html.BeginForm("Edit", "Booking"))
{
@Html.HiddenFor(x => x.id)
<tr>
<td>
<span class="display-field">@b.forenames</span>
</td>
<td>
<span class="display-field">@b.surname</span>
</td>
<td>
<span class="display-field">@String.Format("{0:dd/MM/yyyy}", b.dob)</span>
</td>
<td>
@Html.DropDownListFor(model => b.attendanceStatusId, Model.allAttendances)
</td>
<td>
<input type="submit" value="Save" />
</td>
</tr>
}
}
}
</table>
}
</fieldset>
So the view accepts the container view model BookingIndexViewModel
and creates a form for each BookingViewModel
held. When submitted I expect it to pass back the container view model with the modified BookingViewModel
but it's always empty. I've also tried expecting the single BookingViewModel
that is modified with the same result. Does MVC submit a hydrated view model of the same type as the type given to the view or the type inferred from within the form block?
The only values of importance that I retrieve from the view is the modified attendanceId
(that should be filled by the dropdown helper) and the booking id.