[英]MVC 4 Model binding with partial view
@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>
}
我有呈现此部分视图的这种形式:
@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>
控制器动作:
public ActionResult PrintDoorSigns(DateTime SelectedDate, DoorSignsFormVM Model)
当我提交表单时,隐藏的输入“ SelectedDate”被很好地传递回来,并且包含两个IEnumerable变量的Model变量不为null。 我希望列表中的一个为null,因为不应将其传递回去,而我希望填充的SelectedRooms变量是计数为0的新列表。
我认为此属性上的绑定只是错误的,但是我不明白为什么,任何指针? 谢谢
编辑:
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);
}
这是在表单中呈现的doorsignsform动作。
如您所说,ASP.NET MVC无法将复选框值识别为DoorSignsFormVM
视图模型的一部分。
鉴于您的SelectedRooms
属性是SelectListItem
的集合,因此MVC无法识别如何将复选框中的字符串值绑定到此属性。
尝试将另一个名为string []类型的SelectedRoomValues属性添加到视图模型,并将复选框代码更改为
@Html.CheckBoxListFor(model => model.SelectedRoomValues, model => model.Rooms, entity => entity.Value, entity => entity.Text, model => model.SelectedRooms, htmlListInfo)
然后,MVC将知道如何绑定到此新属性,该属性将用SelectListItem.Value
值填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.