将嵌套类列表从View绑定到Controller

时间:2017-04-04 07:56:37

标签: c# asp.net-mvc model-view-controller

我需要将嵌套类中的List绑定到ActionMethod。

现在,在调用ActionMethod(如下所示)时,ActionItemList是 null

不幸的是我无法将List移动​​到主模型中。

这是我的主要模特:

public class StateViewModel
{
    public EmergencyOperationActionListModel ActionListModel { get; set; }
    public EmergencyInfoModel InfoModel
    public EmergencyInfoCauseListModel CauseListModel { get; set; }
}

和嵌套的:

public class EmergencyInterventiActionListModel
{
    public string Firefighters { get; set; }
    public string ExternalAssistance { get; set; }
    public string PlacesDescription { get; set; }
    public List<ActionItemModel> ActionItemList { get; set; }
}

观点:

        @model Emergencies.Models.StatoViewModel


        //...code code code

        @using (Html.BeginForm("EditOps", "Operations")
        {

            <table class="table table-responsive table-hover" style="margin-bottom: 0px;">
                <thead class="headOperations">
                    <tr>
                        <td>
                            CompanyName
                        </td>
                        <td>
                            Workers
                        </td>
                        <td>
                            Due Date
                        </td>
                        <td>
                           Start Date
                        </td>
                        <td>
                            End Date
                        </td>
                        <td>
                           Action
                        </td>
                    </tr>
                </thead>
                @if (Model.ActionListModel != null)
                {
                    for (int i = 0; i < Model.ActionListModel.ActionItemList.Count(); i++)
                    {
                        <tr>
                            <td>
                                @Html.HiddenFor(m=>m.ActionListModel.ActionItemList[i].Id)

                                @Html.EditorFor(m => m.ActionListModel.ActionItemList[i].CompanyName, new { htmlAttributes = new { id = "companyEdit_" + Model.ActionListModel.ActionItemList[i].Id, @class = "editBoxForInterventi" } })
                            </td>
                            <td>
                                @Html.EditorFor(m => m.ActionListModel.ActionItemList[i].NumberOfWorkers, new { htmlAttributes = new { id = "workersEdit_" + Model.ActionListModel.ActionItemList[i].Id, style = "width:40px", @class = "editBoxForInterventi" } })
                            </td>
                            <td>
                                <div class="input-group date" id="duedateCalendar_@Model.ActionListModel.ActionItemList[i].Id">
                                    @Html.EditorFor(m => m.ActionListModel.ActionItemList[i].DueDate, new { htmlAttributes = new { onclick = "CalendarDue('" + Model.ActionListModel.ActionItemList[i].Id + "')", id = "duedateEdit_" + Model.ActionListModel.ActionItemList[i].Id, @class = "editBoxForInterventi form-control dueDateCalendar", @readonly = "readonly" } })
                                </div>
                            </td>
                            <td>
                                <div class="input-group date" id="starttimeCalendar_@Model.ActionListModel.ActionItemList[i].Id">
                                    @Html.EditorFor(m => m.ActionListModel.ActionItemList[i].StartTime, new { htmlAttributes = new { id = "starttimeEdit_" + Model.ActionListModel.ActionItemList[i].Id, @class = "editBoxForInterventi  form-control starttimeCalendar", @readonly = "readonly" } })
                                </div>
                            </td>
                            <td>
                                <div class="input-group date" id="endtimeCalendar_@Model.ActionListModel.ActionItemList[i].Id">
                                    @Html.EditorFor(m => m.ActionListModel.ActionItemList[i].EndTime, new { htmlAttributes = new { id = "endtimeEdit_" + Model.ActionListModel.ActionItemList[i].Id, @class = "editBoxForInterventi  form-control endtimeCalendar", @readonly = "readonly" } })
                                </div>
                            </td>
                            <td></td>
                        </tr>
                    }
                }
            </table>
            <button type="submit" class="submit-with-icon btn btn-flussi-add" name="doButton" value="save">
                <span class="glyphicon glyphicon-pencil"></span>
            </button>
        }

        //code code code...

最后是控制器:

  public ActionResult EditOps( List<ActionItemModel> ActionItemList )
{
    //code
}

1 个答案:

答案 0 :(得分:1)

只需更改您的EditOps方法签名即可拥有以下内容。

public ActionResult EditOps(StateViewModel modelPosted)
{
    //access modelPosted here.
}

根据视图中的@model指令序列化整个模型。