0

I need to bind a List inside a nested class to my ActionMethod.

Now, on calling the ActionMethod (look below), ActionItemList is null

Unfortunately i can't move the List into the main Model.

This is my main Model:

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

and the nested one:

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

The view:

        @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="[email protected][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="[email protected][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="[email protected][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...

And finally the controller:

  public ActionResult EditOps( List<ActionItemModel> ActionItemList )
{
    //code
}
6
  • 1
    Your form should be bound to the model itself. Change the type to StateViewModel in EditOps. Commented Apr 4, 2017 at 8:07
  • Forget to mention: i have (and use) other properties in StateViewModel, so i can't change it. Commented Apr 4, 2017 at 8:13
  • 1
    I didn't mean change the model... Change the signature of your EditOps method! Your view is bound to the model as per the @model directive at the top of the page. This is what the form data will be serialized into when posted! i.e. EditOps( StateViewModel modelPosted ) Commented Apr 4, 2017 at 8:17
  • Thanks @Wheels73. Following your advice i changed the signature from List<ActionItemModel> ActionItemList to EmergencyOperationActionListModel ActionListModel, and now it works. Ofc the names must match. Commented Apr 4, 2017 at 8:58
  • You're welcome... I'll post it as an answer for you then. Cheers Commented Apr 4, 2017 at 9:02

1 Answer 1

1

Just change your EditOps method signature to have the below.

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

The whole model is serialized as per the @model directive in your view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.