简体   繁体   English

具有部分视图的MVC 4模型绑定

[英]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>
}

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. 当我提交表单时,隐藏的输入“ SelectedDate”被很好地传递回来,并且包含两个IEnumerable变量的Model变量不为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. 我希望列表中的一个为null,因为不应将其传递回去,而我希望填充的SelectedRooms变量是计数为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. 这是在表单中呈现的doorsignsform动作。

As you say, ASP.NET MVC is not recognizing the checkbox values as being part of the DoorSignsFormVM view model. 如您所说,ASP.NET MVC无法将复选框值识别为DoorSignsFormVM视图模型的一部分。

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. 鉴于您的SelectedRooms属性是SelectListItem的集合,因此MVC无法识别如何将复选框中的字符串值绑定到此属性。

Try adding another property called SelectedRoomValues of type string[] to your viewmodel and change your checkbox code to 尝试将另一个名为string []类型的SelectedRoomValues属性添加到视图模型,并将复选框代码更改为

@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. 然后,MVC将知道如何绑定到此新属性,该属性将用SelectListItem.Value值填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM