title: Inserting multiple rows of data in database MVC
body: <p>Currently, I am able to insert my items for one row to the database however, the only issue that I'm running into is that if I add another row of data it does not save the other row just the first one. I'm pretty sure the GET action is correct, I believe the problem lies in the POST action but I'm not sure. Any help is very much appreciated.</p>
  
  <p>This is the code that I have so far:</p>
  
  <p><strong><em>Controller</em></strong></p>
  
  <pre><code>    [HttpGet]
  
      public ActionResult InsertData(int fiscalYear, int period, int locationID)
      {
         testViewModel viewModel = new testViewModel();
  
          using (BusinessLogic BLL = new BusinessLogic())
          {
  
  
              List&lt;DataList&gt; data = BLL.GetData(fiscalYear, period, locationID);
              List&lt;List&lt;DataDetail&gt;&gt; selecteddata = new List&lt;List&lt;DataDetail&gt;&gt;();
  
              selecteddata.Add(BLL.GetDataList(fiscalYear, period, locationID));
  
              viewModel.Selecteddata = selecteddata;
              viewModel.isFirstLoad = false;
  
          }
  
          return PartialView("_dataView", viewModel);
  
      }
  
  
      [HttpPost]
      public ActionResult InsertData(List&lt;DataList&gt; item)
      {
  
          using (BusinessLogic BLL = new BusinessLogic())
          {
  
  
              if (ModelState.IsValid)
              {
                  try
                  {
  
                      foreach(DataList item in items)
                      {                 
                      BLL.InsertDataItem(item.FiscalYear, item.Period, item.LocationID, item.Status, item.Comment);
                      BLL.SaveChanges();
  
                  }
                  }
                  catch (Exception ex)
                  {
  
                      ModelState.AddModelError("", ex);
  
                  }
              }
  
              return RedirectToAction("Index");
  
  
          }
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong><em>View</em></strong></p>
  
  <pre><code>  @using (Html.BeginForm())
    {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
      &lt;br /&gt;    
          &lt;div class="panel-body"&gt;
              &lt;table class="table table-bordered table-condensed" id="tab_logic"&gt;
                  &lt;thead&gt;
                      &lt;tr class="filters"&gt;
                          &lt;th&gt;Status&lt;/th&gt;
                          &lt;th&gt;Comments&lt;/th&gt;
  
                      &lt;/tr&gt;
                  &lt;/thead&gt;
  
                  &lt;tbody&gt;
                      &lt;tr id="addItem"&gt;
  
                          &lt;td&gt;&lt;input type="text" id="Status" name="Status" class="form-control" /&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" id="Comments" name="Comments" class="form-control" /&gt;&lt;/td&gt;                   
                      &lt;/tr&gt;
                  &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/div&gt;
          &lt;a id="add_row" class="btn btn-default pull-left"&gt;Add Row&lt;/a&gt;&lt;a id='delete_row' class="pull-right btn btn-default"&gt;Delete Row&lt;/a&gt;
      &lt;/div&gt;
  
      &lt;div class=""&gt;
  
          &lt;input type="submit" value="Save" class="btn btn-success btn-block" /&gt;
      &lt;/div&gt;
  
  }
  </code></pre>
  
  <p><strong>javascript</strong></p>
  
  <pre><code>$(document).ready(function () {
          var i = 1;
          $("#add_row").click(function () {
              $('#addItem' + i).html("&lt;td&gt;&lt;input name='ActionItem" + i + "' type='text' class='form-control'  /&gt; &lt;/td&gt;&lt;td&gt;&lt;input  name='Asignee" + i + "' type='text' class='form-control'&gt;&lt;/td&gt;&lt;td&gt;&lt;input  name='DateAssigned" + i + "' type='date' class='form-control'&gt;&lt;/td&gt;&lt;td&gt;&lt;input  name='DueDate" + i + "' type='date' class='form-control'&gt;&lt;/td&gt;&lt;td&gt;&lt;input  name='Status" + i + "' type='text' class='form-control'&gt;&lt;/td&gt;&lt;td&gt;&lt;input  name='Comments" + i + "' type='text' class='form-control'&gt;&lt;/td&gt;");
  
              $('#tab_logic').append('&lt;tr id="addItem' + (i + 1) + '"&gt;&lt;/tr&gt;');
              i++;
          });
          $("#delete_row").click(function () {
              if (i &gt; 1) {
                  $("#addItem" + (i - 1)).html('');
                  i--;
              }
          });
  
      });
  </code></pre>
  
  <p><strong>--UPDATE--</strong></p>
  
  <p>The "InsertDataItem" method comes from the business logic class. The following code adds the rows</p>
  
  <p><strong><em>Method</em></strong></p>
  
  <pre><code>    public int InsertDataItem(int fiscalYear, int period, int locationID, string status, string comments)
          {
  
              var dataList = GetData(fiscalYear, period, locationID);
              DataList items = new DataList()
              {
  
                  FiscalYear = fiscalYear,
                  Period = period,
                  LocationID = locationID,
  
                  Status = status,
                  Comments = comments
              };
  
              // Inserts row
             entity.DataLists.Add(items);
  
              SaveChanges();
  
              return items.ID;
          }
  </code></pre>