Reputation: 597
@using (Html.BeginForm("PrintDoorSigns", "TimeTable", FormMethod.Post, new { id = "printDoorSigns" }))
{
<input type="hidden" id="doorSignDate" name="SelectedDate" />
<h3>Door Signs</h3>
<fieldset class="float-left">
<legend>Date</legend>
<div id="signsDate"></div>
</fieldset>
<div id="doorSignsRoomList" class="float-left">
@{Html.RenderAction("DoorSignsForm", new { date = DateTime.Now });}
</div>
<div>
<fieldset>
<legend>Options</legend>
<button id="SelectAllRooms">Select All</button>
<button id="RemoveAllRooms">Remove All</button>
</fieldset>
</div>
}
I have this form which renders this partial view:
@model WebUI.ViewModels.CalendarVM.DoorSignsFormVM
<fieldset>
<legend>Rooms</legend>
@{ var htmlListInfo = new HtmlListInfo(HtmlTag.vertical_columns, 3, null, TextLayout.Default, TemplateIsUsed.No);
if (Model.Rooms.Count() > 0)
{
<div id="roomsWithBookings" class="CheckBoxList float-left">
@Html.CheckBoxList("SelectedRooms", model => model.Rooms, entity => entity.Value, entity => entity.Text, model => model.SelectedRooms, htmlListInfo)
</div>
}
}
</fieldset>
Controller action:
public ActionResult PrintDoorSigns(DateTime SelectedDate, DoorSignsFormVM Model)
when I submit the form, the hidden input "SelectedDate" gets passed back fine and the Model variable which contains two IEnumerable variables isn't null. One of the lists is null which I expect, as it shouldn't be passed back and the SelectedRooms variable which I expect to be populated is a new list with count 0.
I assume the binding is just wrong on this property but I don't understand why, any pointers? Thanks
EDIT:
public PartialViewResult DoorSignsForm(DateTime date)
{
var userID = _bookingService.GetCurrentUser(User.Identity.Name);
var model = new DoorSignsFormVM();
model.Rooms = _sharedService.GetRoomsWithBookings(date, userID.FirstOrDefault().DefSite);
return PartialView("_DoorSigns", model);
}
Here is the doorsignsform action that gets rendered in the form.
Upvotes: 0
Views: 1866
Reputation: 394
As you say, ASP.NET MVC is not recognizing the checkbox values as being part of the DoorSignsFormVM
view model.
Given that your SelectedRooms
property is a collection of SelectListItem
s, MVC is not recognizing how to bind the string values from the checkboxes to this property.
Try adding another property called SelectedRoomValues of type string[] to your viewmodel and change your checkbox code to
@Html.CheckBoxListFor(model => model.SelectedRoomValues, model => model.Rooms, entity => entity.Value, entity => entity.Text, model => model.SelectedRooms, htmlListInfo)
MVC will then know how to bind to this new property, which will be populated with the SelectListItem.Value
values.
Upvotes: 2