I have encountered the same problem, with a null object ("on save") after running a foreach loop. I changed the foreach loop to a "for" loop and the problem went away.
My Original code:
@foreach (var item in Model.ContractorLocationData)
{
<tr>
<td>@Html.DropDownListFor(m => item.AddressType, WebMXT.MVC.Controllers.SystemManagementController.GetBusinessLocationType(), item.AddressType)</td>
<td>@Html.EditorFor(m => item.AddressLine1)</td>
<td>@Html.EditorFor(m => item.AddressLine2)</td>
<td>@Html.EditorFor(m => item.City)</td>
<td>@Html.DropDownListFor(m => item.StateCode, WebMXT.MVC.Controllers.SystemManagementController.GetStateCodeDropDown(), item.StateCode)</td>
<td>@Html.EditorFor(m => item.ZipCode)</td>
<td>@Html.DropDownListFor(m => item.CountryCode, WebMXT.MVC.Controllers.SystemManagementController.GetCountryCodeDropDown(), item.CountryCode, new { style = "max-width: 200px" })</td>
<td>@Html.EditorFor(m => item.PhoneNumber)</td>
<td>@Html.EditorFor(m => item.CommercialFaxNumber)</td>
<td>
@Html.ActionLink(" ", "DeleteLocation",
"SystemManagement",
new { locationSeq = item.ContractorLocationSeq, contractNumber = @Model.CageCode },
new
{
@onclick = "return confirm('Are you sure you want to delete this Location');",
@class = "glyphicon glyphicon-remove"
})
</td>
<td>@Html.HiddenFor(m => item.ContractorLocationSeq)</td>
<td></td>
</tr>
}
Working code:
@for (int i = 0; i < Model.ContractorLocationData.Count; i++)
{
<tr>
<td>@Html.DropDownListFor(m => m.ContractorLocationData[i].AddressType, WebMXT.MVC.Controllers.SystemManagementController.GetBusinessLocationType(), Model.ContractorLocationData[i].AddressType)</td>
<td>@Html.EditorFor(m => m.ContractorLocationData[i].AddressLine1)</td>
<td>@Html.EditorFor(m => m.ContractorLocationData[i].AddressLine2)</td>
<td>@Html.EditorFor(m => m.ContractorLocationData[i].City)</td>
<td>@Html.DropDownListFor(m => m.ContractorLocationData[i].StateCode, WebMXT.MVC.Controllers.SystemManagementController.GetStateCodeDropDown(), Model.ContractorLocationData[i].StateCode)</td>
<td>@Html.EditorFor(m => m.ContractorLocationData[i].ZipCode)</td>
<td>@Html.DropDownListFor(m => m.ContractorLocationData[i].CountryCode, WebMXT.MVC.Controllers.SystemManagementController.GetCountryCodeDropDown(), Model.ContractorLocationData[i].CountryCode, new { style = "max-width: 200px" })</td>
<td>@Html.EditorFor(m => m.ContractorLocationData[i].PhoneNumber)</td>
<td>@Html.EditorFor(m => m.ContractorLocationData[i].CommercialFaxNumber)</td>
<td>
@Html.ActionLink(" ", "DeleteLocation",
"SystemManagement",
new { locationSeq = Model.ContractorLocationData[i].ContractorLocationSeq, contractNumber = @Model.CageCode },
new
{
@onclick = "return confirm('Are you sure you want to delete this Location');",
@class = "glyphicon glyphicon-remove"
})
</td>
<td>@Html.HiddenFor(m => m.ContractorLocationData[i].ContractorLocationSeq)</td>
</tr>
}