asp.net-mvc-5model-bindingmvc-editor-templates

BeginCollectionItem not binding


I've previously used the Html.BeginCollectionItem() extension and had no issues with binding from view to controller. In my current project however I'm having issues.

The model

    public class EditPathViewModel : TradeContractorEscalationPath
    {
         public Guid ContractorId { get; set; }
         public List<ContactDisplayModel> Contacts { get; set; }
         public List<TradeContractorEscalationPathContact> AssignedContacts { get; set; }
         public int maxOrder { get; set; }
    }

   public partial class TradeContractorEscalationPath
   {        
       public System.Guid Id { get; set; }
       public System.Guid TradeContractorId { get; set; }
       public string Description { get; set; }
       public string Notes { get; set; }
       public System.DateTime CreatedDate { get; set; }
       public Nullable<System.DateTime> UpdatedDate { get; set; }        
   }

I am trying to bind AssignedContacts back to the Controller, which looks like...

    [HttpPost]
    public async Task<ActionResult> EditPath([Bind(Include = "Id,TradeContractorId,Description,Notes,ContractorId,AssignedContacts,Contacts")] Models.EditPathViewModel model)
    {
        ...
    }

However, it constantly returns null. The BeginCollectionItem is configured like so:

Editor Template for TradeContractorEscalationPathContact

@model Telecare.POCO.TradeContractorEscalationPathContact

@Html.EditorFor(model => model.Order)
@Html.EditorFor(model => model.TradeContractorEscalationPathId)
@Html.EditorFor(model => model.ContactId)

The view where Assigned Contacts collection item is called

    ...
    <div class="col-lg-6">
        <div class="ibox-title">
            <h5>Contacts</h5>
        </div>
        <div class="ibox-content">
                @Html.EditorFor(model => model.AssignedContacts)
        </div>
    </div>
    ...

Example HTML from DOM when POST is called

...
   <input name="AssignedContacts.index" class="ui-sortable-handle" type="hidden" value="a5895b40-2de5-4ed5-a3ed-137c047963e8" autocomplete="off">
   <input name="AssignedContacts[a5895b40-2de5-4ed5-a3ed-137c047963e8].Order" class="text-box single-line ui-sortable-handle" id="AssignedContacts_a5895b40-2de5-4ed5-a3ed-137c047963e8__Order" type="number" value="1" data-val-required="The Order field is required." data-val="true" data-val-number="The field Order must be a number.">
   <input name="AssignedContacts[a5895b40-2de5-4ed5-a3ed-137c047963e8].TradeContractorEscalationPathId" class="text-box single-line ui-sortable-handle" id="AssignedContacts_a5895b40-2de5-4ed5-a3ed-137c047963e8__TradeContractorEscalationPathId" type="text" value="0807186a-3bad-e811-9c42-8cae4cf36195" data-val-required="The Trade Contractor Path field is required." data-val="true">
   <input name="AssignedContacts[a5895b40-2de5-4ed5-a3ed-137c047963e8].ContactId" class="text-box single-line ui-sortable-handle" id="AssignedContacts_a5895b40-2de5-4ed5-a3ed-137c047963e8__ContactId" type="text" value="1e09c3ae-42ac-e811-9c42-8cae4cf36195" data-val-required="The Contact field is required." data-val="true">
   <input name="AssignedContacts.index" class="ui-sortable-handle" type="hidden" value="974a0c38-642b-40f1-a5bd-dbc4b880eaa2" autocomplete="off">
   <input name="AssignedContacts[974a0c38-642b-40f1-a5bd-dbc4b880eaa2].Order" class="text-box single-line ui-sortable-handle" id="AssignedContacts_974a0c38-642b-40f1-a5bd-dbc4b880eaa2__Order" type="number" value="2" data-val-required="The Order field is required." data-val="true" data-val-number="The field Order must be a number.">
   <input name="AssignedContacts[974a0c38-642b-40f1-a5bd-dbc4b880eaa2].TradeContractorEscalationPathId" class="text-box single-line ui-sortable-handle" id="AssignedContacts_974a0c38-642b-40f1-a5bd-dbc4b880eaa2__TradeContractorEscalationPathId" type="text" value="0807186a-3bad-e811-9c42-8cae4cf36195" data-val-required="The Trade Contractor Path field is required." data-val="true">
   <input name="AssignedContacts[974a0c38-642b-40f1-a5bd-dbc4b880eaa2].ContactId" class="text-box single-line ui-sortable-handle" id="AssignedContacts_974a0c38-642b-40f1-a5bd-dbc4b880eaa2__ContactId" type="text" value="f9d8afdd-42ac-e811-9c42-8cae4cf36195" data-val-required="The Contact field is required." data-val="true">
   <input name="AssignedContacts.index" type="hidden" value="75a5e8e5-82e1-4f58-b185-ca6abefc5d9c" autocomplete="off">
   <input name="AssignedContacts[75a5e8e5-82e1-4f58-b185-ca6abefc5d9c].Order" class="text-box single-line" id="AssignedContacts_75a5e8e5-82e1-4f58-b185-ca6abefc5d9c__Order" type="number" value="3" data-val-required="The Order field is required." data-val="true" data-val-number="The field Order must be a number.">
   <input name="AssignedContacts[75a5e8e5-82e1-4f58-b185-ca6abefc5d9c].TradeContractorEscalationPathId" class="text-box single-line" id="AssignedContacts_75a5e8e5-82e1-4f58-b185-ca6abefc5d9c__TradeContractorEscalationPathId" type="text" value="0807186a-3bad-e811-9c42-8cae4cf36195" data-val-required="The Trade Contractor Path field is required." data-val="true">
   <input name="AssignedContacts[75a5e8e5-82e1-4f58-b185-ca6abefc5d9c].ContactId" class="text-box single-line" id="AssignedContacts_75a5e8e5-82e1-4f58-b185-ca6abefc5d9c__ContactId" type="text" value="9ea0dcf8-42ac-e811-9c42-8cae4cf36195" data-val-required="The Contact field is required." data-val="true">
   <input name="AssignedContacts.index" type="hidden" value="965b81be-f52c-4055-8b4b-bccf916f14a4" autocomplete="off">
   <input name="AssignedContacts[965b81be-f52c-4055-8b4b-bccf916f14a4].Order" class="text-box single-line" id="AssignedContacts_965b81be-f52c-4055-8b4b-bccf916f14a4__Order" type="number" value="4" data-val-required="The Order field is required." data-val="true" data-val-number="The field Order must be a number.">
   <input name="AssignedContacts[965b81be-f52c-4055-8b4b-bccf916f14a4].TradeContractorEscalationPathId" class="text-box single-line" id="AssignedContacts_965b81be-f52c-4055-8b4b-bccf916f14a4__TradeContractorEscalationPathId" type="text" value="0807186a-3bad-e811-9c42-8cae4cf36195" data-val-required="The Trade Contractor Path field is required." data-val="true">
   <input name="AssignedContacts[965b81be-f52c-4055-8b4b-bccf916f14a4].ContactId" class="text-box single-line" id="AssignedContacts_965b81be-f52c-4055-8b4b-bccf916f14a4__ContactId" type="text" value="fc7a9669-43ac-e811-9c42-8cae4cf36195" data-val-required="The Contact field is required." data-val="true">
...

From all the threads I've read - the HTML looks like it should correctly bind so I must be missing something or doing something wrong elsewhere? Does anyone have any ideas?

Thanks in advance


Solution

  • OK - turns out I'm an idiot. The code above does indeed work but it wasn't binding because it wasn't within the @Html.BeginForm()) section. Sigh.