0

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

Toxicable
  • 1,529
  • 1
  • 19
  • 28
  • 1
    It has nothing to do with the `id` (a form only posts back `name/value` pairs based on the `name` and `value` attributes. Its because you controls are not named correctly with indexers. Refer some examples [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for dynamically adding collection items –  Jan 12 '16 at 07:33
  • @Stephen Muecke Right that makes a lot more sense thanks, so would doing something like ``name="Email' + new Date() + ' "`` for the name tag on the fields, would that then submit to the list of CompanyContact? – Toxicable Jan 12 '16 at 07:43
  • 1
    Study the links :) - In order to bind it needs to `name="CompanyContactList[].Email"` where `` is the indexer in the collection. The collection indexers need to start at zero and be consecutive, or you can add a hidden input - `` to allow non consecutive indexers –  Jan 12 '16 at 07:54
  • @StephenMuecke OK I got it working, I made a var outside of the function that increments each time the function is called so that we get the [0] .. [1] and model bound itself to it :D Thanks heaps for the help – Toxicable Jan 12 '16 at 08:18
  • The disadvantage of that is that you cannot delete items in the collection (the indexers would then be non consecutive) so best to use the hidden input for the Indexer as per the links –  Jan 12 '16 at 08:23
  • @StephenMuecke Yeah I think you're right there, however wouldn't you make it so that delete the highest index? (just a guess). I don't think that'll be an issue in my case however and im just happy it's working for now. My next issue will be with validation, EG if user clicks submit and it fails server validation which will reload the page and remove the fields but i'll look around for a a fix – Toxicable Jan 12 '16 at 10:03
  • 1
    I have that covered in [this answer](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) :) –  Jan 12 '16 at 10:04
  • On second thought I guess I can just pass the data model back to the view ``if (CompanyContactList != null) { ..display fields..}`` but then i'd have to make sure the index starts at the right palce – Toxicable Jan 12 '16 at 10:06
  • @StephenMuecke Ahhh awesome, thanks heaps again, i'll be referring back to that when I get back to it – Toxicable Jan 12 '16 at 10:09

0 Answers0