I'm trying to pass my model back to a controller. I have included all fields of the model in the form but two parts of the model are getting lost once it makes it back to the controller, Employees and EmployeesInMultipleCompanies, which are both of type IList. I have verified that the fields are present when they are passed to the view, they just don't make it back to the controller.
.cshtml
@using (Html.BeginForm("PostEmail","Import",FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>EmailViewModel</legend>
<p>
<input type="email" name="EmailAddresses" value=" " required="required" />
<span class="btn_orange"><a href="#" class="remove_field" >x</a></span>
</p>
<p>
<span class="btn_orange"><a class="add_email_button" href="#">Add Another Email</a></span>
</p>
@Html.HiddenFor(m=> Model.Employees)
@Html.HiddenFor(m=> Model.CompanyId)
@Html.HiddenFor(m=> Model.CompanyName)
@Html.HiddenFor(m=> Model.PayFrequency)
@Html.HiddenFor(m=> Model.FirstPayPeriodBeginDate)
@Html.HiddenFor(m=> Model.LastPayPeriodEndDate)
@Html.HiddenFor(m=> Model.NumberPayPeriods)
@Html.HiddenFor(m=> Model.ValidEmployees)
@Html.HiddenFor(m=> Model.InvalidEmployees)
@Html.HiddenFor(m=> Model.EmployeesInMultipleCompanies)
@Html.HiddenFor(m=> Model.TotalEmployeeCount)
<p>
<input type="submit" value="Send Email" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Cancel", "Continue", "Import")
</div>
<script type="text/javascript">
$(document).ready(function () {
$('body').on('click', '.remove_field', function () {
$(this).closest('p').remove();
});
$('.add_email_button').closest('p').click(function () {
var html = '<p><input type="email" required="required" name="EmailAddresses" /><span class="btn_orange"><a href="#" class="remove_field">x</a></span></p>';
$(html).insertBefore($(this));
});
$(body).on('click', 'submit', function() {
$('email').attr('required', true);
});
});
</script>
controller .cs
public ActionResult SendEmail(ImportViewModel model)
{
var editedEmployees = model.EmployeesInMultipleCompanies;
var importModel = TempData["ImportModel"] as ImportViewModel;
//for each employee who is in multiple companies, set user-chosen company id and the COHD related to that company
var importService = new ImportService();
importService.UpdateEmployeesInMultipleCompanies(editedEmployees, importModel.Employees);
TempData["ImportModel"] = importModel;
return this.RazorView("SendEmail", importModel);
}
[HttpPost]
public ActionResult PostEmail(ImportViewModel model)
{
IEmailer emailer = new Emailer();
emailer.SendEmail(model.EmailAddresses, model.Employees);
var employees =
model.Employees.Where(e => !string.IsNullOrWhiteSpace(e.ValidationErrorOrException)).ToList();
var emailViewModel = new EmailViewModel(employees);
return this.RazorView("Continue", emailViewModel);
}