title: dynamically create textbox and save data MVC
body: <p>I am new to MVC. I want to dynamically create textboxes upon user click. </p>
  
  <pre><code>&lt;script&gt;
  function addRow(tableID) {
      var table = document.getElementById(tableID);
      var rowCount = table.rows.length;
      var row = table.insertRow(rowCount);
      var colCount = table.rows[0].cells.length;
          for(var i=0; i&lt;colCount; i++) {
  
              var newcell = row.insertCell(i);
              newcell.innerHTML = table.rows[0].cells[i].innerHTML;
              switch(newcell.childNodes[0].type) {
                  case "text":
                      newcell.childNodes[0].value = "";
                      break;
                  case "checkbox":
                      newcell.childNodes[0].checked = false;
                      break;
                  case "select":
                      newcell.childNodes[0].selectedIndex = 0;
                      clear_attrib();
                      break;
          }
  
      }
      else {
          alert("Cannot add another row.");
      }
  }
  
  function deleteRow(tableID) {
      try {
          var table = document.getElementById(tableID);
          var rowCount = table.rows.length;
          for(var i=0; i&lt;rowCount; i++) {
              var row = table.rows[i];
              var chkbox = row.cells[0].childNodes[0];
              if(null != chkbox &amp;&amp; true == chkbox.checked) {
                  if(rowCount &lt;= 1) {
                      alert("Cannot delete all the rows.");
                      break;
                  }
                  table.deleteRow(i);
                  rowCount--;
                  i--;
              }
          }
      }catch(e) {
          alert(e);
      }
  }
  </code></pre>
  
  <p></p>
  
  <p>and the Razor</p>
  
  <pre><code>&lt;table class="table" id="AddSchedule"&gt;
    &lt;tr&gt;
        &lt;td&gt;&lt;input type="checkbox" name="chk[]" class="checkbox_style" /&gt;&lt;/td&gt;
        &lt;td&gt;@Html.EditorFor(model =&gt; model.Schedule, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model =&gt; model.Schedule, "", new { @class = "text-danger" })
         &lt;/td&gt;
         &lt;td&gt;@Html.EditorFor(model =&gt; model.Room, new { htmlAttributes = new { @class = "form-control" } })
             @Html.ValidationMessageFor(model =&gt; model.Room, "", new { @class = "text-danger" })
          &lt;/td&gt;
          &lt;td&gt;@Html.EditorFor(model =&gt; model.Subject, new { htmlAttributes = new { @class = "form-control" } })
              @Html.ValidationMessageFor(model =&gt; model.Subject, "", new { @class = "text-danger" })
           &lt;/td&gt;
      &lt;/tr&gt;
  </code></pre>
  
  <p></p>
  
  
        
        
        
         
  
  
  <p>the adding and removing of textboxes is OK. But I don't know how to save the data in the database. This is my controller</p>
  
  <pre><code>    public ActionResult Create(EmployeeViewModel employeeViewModel) 
  {
  if (ModelState.IsValid)
      {
        var emp = db.Employee.Create();
        emp.ID = employeeViewModel.ID;
        emp.LastName = employeeViewModel.LastName;
        emp.FirstName = employeeViewModel.FirstName;
        db.Employee.Add(emp);
  
        var sched = db.FacultySchedule.Create();
        sched.Schedule = employeeViewModel.Schedule;
        sched.Room = employeeViewModel.Room;
        sched.Subject = employeeViewModel.Subject;
  
        db.FacultySchedule.Add(sched);
        db.SaveChanges();
  }
  </code></pre>
  
  <p>I tried using foreach but it still save only the first value, so i removed it... I think I'm missing something in the mark-up... like adding <code>[]</code> to make it an array just like in PHP. And I don't know how to fix it in the controller.</p>
  
  <p>How can I fix the mark-up and the loop for in the controller for saving? Thanks.</p>
  
  <p><strong>Edit</strong>
  this is the viewmodel</p>
  
  <pre><code>public class EmployeeViewModel
  {
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public int ID { get; set; }
  
      public string LastName { get; set; }
      public string FirstName { get; set; }
      public string Schedule { get; set; }
      public string Room { get; set; }
      public string Subject { get; set; }
      public virtual IEnumerable&lt;FacultySchedule&gt; FacultySchedule { get; set; }
  </code></pre>
  
  <p>}</p>
  
  <p>and these are the domain model</p>
  
  <pre><code>public partial class Employee
  {
      public Employee()
      {
          FacultySchedule = new HashSet&lt;FacultySchedule&gt;();
      }
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public int ID { get; set; }
      public string LastName { get; set; }
      public string FirstName { get; set; }
  
      public virtual ICollection&lt;FacultySchedule&gt; FacultySchedule { get; set; }
  
  }
  
  public partial class FacultySchedule
  {
      public FacultySchedule()
      {
          Substitution = new HashSet&lt;Substitution&gt;();
      }
      public int ID { get; set; }
      public int EmployeeID { get; set; }
      public string Schedule { get; set; }
      public string Room { get; set; }
      public string Subject { get; set; }
  
      public virtual Employee Employee { get; set; }
  }
  </code></pre>