So I am trying to make it so that the user can add as many contacts as they want to this form, I have some code but I don't really know what i'm doing, some direction would be nice.
Models
public class CreateCompanyViewModel
{
public string CompanyName { get; set; }
public List<CompanyContact> CompanyContactList { get; set; }
}
public class CompanyContact
{
public string CompanyContactId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
Controller
public ActionResult CreateCompany(CreateCompanyViewModel model)
{
var company = new GroupFitnessCompany {CompanyName = model.CompanyName}
foreach(CompanyContact contact in model.CompanyContactList)
{
company.CompanyContact.Add(contact);
}
HTML/JS on Razor Page
<button id="add-fields" type="button" onclick="appendFields()">Add fields</button>
<div id="extra-fieds"></div>
function appendFields() {
var nameField = '<div class="form-group"><label for="CompantContact.Name">Contact Name</label><div ><input id="CompantContact.Name" name="CompantContact.Name" type="text" value="">'
var emailField = '<div class="form-group"><label for="CompantContact.Email">Contact Email</label><div ><input id="CompantContact.Email" name="CompantContact.Email" type="text" value="">'
div.innerHTML += nameField;
div.innerHTML += emailField;
}
Sorry about the really long lines, horrible to read
So that code works at placing the fields on the page but when I hit submit they don't get passed through to the controller, i'm guessing it's because of my id's? im not sure what im meant to called them