【问题标题】:MVC 4 Model binding with partial view带有局部视图的 MVC 4 模型绑定
【发布时间】:2014-06-26 20:52:02
【问题描述】:
@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 变量的模型变量不为空。其中一个列表是我期望的 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 操作。

【问题讨论】:

  • 您不应该将 DoorSignsFormVM 模型传递给此处的 RenderAction 方法 Html.RenderAction("DoorSignsForm", new { date = DateTime.Now }) 吗?您能否发布 DoorSignsForm 操作的代码?
  • 已将其添加到主要问题中,谢谢

标签: c# asp.net-mvc asp.net-mvc-4 binding partial-views


【解决方案1】:

正如你所说,ASP.NET MVC 没有将复选框值识别为 DoorSignsFormVM 视图模型的一部分。

鉴于您的 SelectedRooms 属性是 SelectListItems 的集合,MVC 无法识别如何将复选框中的字符串值绑定到此属性。

尝试将另一个名为 SelectedRoomValues 类型为 string[] 的属性添加到您的视图模型中,并将您的复选框代码更改为

@Html.CheckBoxListFor(model => model.SelectedRoomValues, model => model.Rooms, entity => entity.Value, entity => entity.Text, model => model.SelectedRooms, htmlListInfo)

然后,MVC 将知道如何绑定到这个新属性,该属性将填充 SelectListItem.Value 值。

【讨论】:

  • 我试过了,但是当我将其更改为“DoorSignsFormVM.SelectedRooms”时,它所做的只是让控制器操作中的对象有两个空列表,而不是一个空列表和一个计数为 0 的列表。遗憾的是,您也不能将 lambda 用于第一个参数,因为它抱怨它需要一个字符串 :(
  • 您是否尝试过使用采用 lambda 而不是字符串的 @Html.CheckBoxList 的重载?
  • 您还可以将视图模型上的 SelectedRooms 属性从 Room 对象的集合更改为字符串数组,看看是否可行。
  • 所有 16 个重载都期望 listName 的字符串作为第一个参数
  • 尝试添加另一个名为SelectedRoomValues 类型为string[] 的属性并将您的复选框代码更改为@Html.CheckBoxListFor(model =&gt; model.SelectedRoomValues, model =&gt; model.Rooms, entity =&gt; entity.Value, entity =&gt; entity.Text, model =&gt; model.SelectedRooms, htmlListInfo)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
相关资源
最近更新 更多