title: Insert Multiple Records MVC
body: <p>I am working on inserting multiple records in MVC 5.  I have been using the tutorial here as a start: <a href="http://dotnetawesome.com/mvc/insert-multiple-record-to-database-at-a-time-aspnet-mvc4" rel="nofollow">http://dotnetawesome.com/mvc/insert-multiple-record-to-database-at-a-time-aspnet-mvc4</a>, but my situation is a bit different.</p>
  
  <p>I am pulling data from an existing table, then adding 4 columns of data.  Both the data from the existing table and the new 4 columns of data will be added into a new table.  I have no need for the "Add new rows" as shown in the tutorial above.</p>
  
  <p>I have everything set up, but in my post my model is coming back blank, and I am not sure why. </p>
  
  <pre><code>// GET:
  public ActionResult PCP2(string sortOrder, string currentFilter, string searchString, int? page, string pcp, string location, string locationToSchedule, string scheduleVisitType, string approvedBy)
  {
      ViewBag.Location = location;
      ViewBag.LocationType = locationToSchedule;
      ViewBag.VisitType = scheduleVisitType;
      ViewBag.ApprovedBy = approvedBy;
  
      //query db   
      var model = from n in db.DataDump
                  where n.PCP == pcp
                  orderby n.MemberName
                  select new SnapshotVM()
                  {
                      SubscriberID = n.SubscriberID,
                      MemberName = n.MemberName,
                      DateOfBirth = n.DateOfBirth,
                      PCP = n.PCP,
                      IPA = n.IPA,
                      App = n.App,
                      RemainingOpportunities = n.RemainingOpportunities,
                      TID = 0,
                      Location = "",
                      ApprovedBy = "",
                      LocationToSchedule = "",
                      ScheduleVisitType = "",
                      Guid = Guid.NewGuid(),
                      ParentCompany = "XXX",
                      location = location,
                      locationToSchedule = locationToSchedule,
                      scheduleVisitType = scheduleVisitType,
                      approvedBy = approvedBy
                  };
  
      return View(model.ToList());
  }
  
  // POST: 
  [HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult PCP(List&lt;SnapshotVM&gt; model)
  {
      var addMember = newTD();            
      // save data
      using (var context = new TDAllDb())
          foreach (var item in model)                     
          {
              //Add Member                    
              addMember.SubscriberID = item.SubscriberID;
              addMember.MemberName = item.MemberName;
              addMember.DateOfBirth = item.DateOfBirth;
              addMember.PCP = item.PCP;
              addMember.IPA = item.IPA;
              addMember.App = item.App;
              addMember.RemainingOpportunities = item.RemainingOpportunities;
              addMember.Location = item.Location;
              addMember.ApprovedBy = item.ApprovedBy;
              addMember.LocationToSchedule = item.LocationToSchedule;
              addMember.ScheduleVisitType = item.ScheduleVisitType;
              addMember.Guid = Guid.NewGuid();
              addMember.DateCreated = System.DateTime.Now.AddHours(-7);
              addMember.ParentCompany = "XXX";
              context.TD.Add(addMember);
              context.SaveChanges();          
          }                  
  
      return View(model);
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;table&gt;
  @if (Model != null &amp;&amp; Model.Count &gt; 0)
  {
      int j= 0;
      foreach (var item in Model)
      {
          j = j + 1;
          &lt;tr&gt;
              &lt;td&gt;@j&lt;/td&gt;
              &lt;td&gt;
                  @item.SubscriberID
              &lt;/td&gt;
              &lt;td&gt;
                  @item.MemberName
              &lt;/td&gt;
              &lt;td&gt;
                  @if (item.DateOfBirth != null)
                  {
                      @item.DateOfBirth.Value.ToShortDateString()
                  }
              &lt;/td&gt;
              &lt;td&gt;
                  @item.IPA
              &lt;/td&gt;
              &lt;td&gt;
                  @item.App
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DropDownList("[" + @j + "].Location", (IEnumerable&lt;SelectListItem&gt;)ViewBag.location, "", new { @class = "input-sm" })
                  @Html.Hidden("[" + @j + "].SubscriberID", item.SubscriberID)
                  @Html.Hidden("[" + @j + "].MemberName", item.MemberName)
                  @Html.Hidden("[" + @j + "].DateOfBirth", item.DateOfBirth)
                  @Html.Hidden("[" + @j + "].PCP", item.PCP)
                  @Html.Hidden("[" + @j + "].IPA", item.IPA)
                  @Html.Hidden("[" + @j + "].App", item.App)
                  @Html.Hidden("[" + @j + "].RemainingOpportunities", item.RemainingOpportunities)
                  &lt;td&gt;
                      @Html.DropDownList("[" + @j + "].LocationToSchedule", new List&lt;SelectListItem&gt;
                      {
                          new SelectListItem {Text = "Home", Value = "Home"},
                          new SelectListItem {Text = "Office", Value = "Office"},
                      }, 
                      "", 
                      new { @class = "form-control" })
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DropDownList("[" + @j + "].ScheduleVisitType", new List&lt;SelectListItem&gt;
                      {
                          new SelectListItem {Text = "Screening", Value = "Screening"},
                          new SelectListItem {Text = "Hold", Value = "Hold"},
                      }, 
                      "", 
                      new { @class = "form-control" })
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DropDownList("[" + @j + "].ApprovedBy", new List&lt;SelectListItem&gt;
                      {
                          new SelectListItem {Text = "XXX", Value = "XXX"},
                          new SelectListItem {Text = "YYY", Value = "YYY"},
                      },
                      "", 
                      new { @class = "form-control" })
                  &lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  }
  &lt;/table&gt;
  &lt;input type="submit" value="Save Changes" class="btn btn-success" /&gt;
  </code></pre>
  
  <p>ViewModel</p>
  
  <pre><code>public class SnapshotVM
  {
      [Key]
      [Required]
      [StringLength(255)]
      [DisplayName("Subscriber ID")]
      public string SubscriberID { get; set; }
      [StringLength(255)]
      public string IPA { get; set; }
      [StringLength(255)]
      [DisplayName("Member Name")]
      public string MemberName { get; set; }
      [DisplayName("DOB")]
      public DateTime? DateOfBirth { get; set; }
      [StringLength(255)]
      public string App { get; set; }
      [DisplayName("Rem. Opp.")]
      public int? RemainingOpportunities { get; set; }
      [StringLength(255)]
      public string PCP { get; set; }
  
      [Key]       
      public int? TID { get; set; }
      [Required]
      [StringLength(50)]
      [DisplayName("*Location")]
      public string Location { get; set; }
      [DisplayName("Parent Company")]
      public string ParentCompany { get; set; }        
      [StringLength(255)]
      [DisplayName("Approved By")]
      public string ApprovedBy { get; set; }        
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public Guid? Guid { get; set; }
      [StringLength(25)]
      [DisplayName("Type of Visit to Schedule")]
      public string ScheduleVisitType { get; set; }
      [StringLength(15)]
      [DisplayName("Location to Schedule")]
      public string LocationToSchedule { get; set; }
      public DateTime? DateCreated { get; set; }
      public IEnumerable&lt;SelectListItem&gt; PCPList { get; set; }
      public string location { get; set; }
      public string locationToSchedule { get; set; }
      public string scheduleVisitType { get; set; }
      public string approvedBy { get; set; }
  }
  </code></pre>
  
  <p>The error that I am receiving is:</p>
  
  <pre><code>{"Object reference not set to an instance of an object."}
  </code></pre>
  
  <p>in the "Post" on the model...           </p>
  
  <pre><code>foreach (var item in model)
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p>TIA!  </p>
  
  <hr>
  
  <p>EDIT</p>
  
  <p>I updated my field to HiddenFor and DropDownListFor (ex: @Html.HiddenFor(m => item.SubscriberID)) and I am getting a new error:</p>
  
  <pre><code>System.StackOverflowException was unhandled
  Message: An unhandled exception of type 'System.StackOverflowException' occurred in System.Web.dll
  
  Make sure you don't have an infinite loop or recursion.
  </code></pre>
  
  <p>This error occurs on the "Get", when I am selecting options in my dropdown menus.  I noted in the source that the fields are not indexing with a count.</p>
  
  <pre><code> @if (Model != null &amp;&amp; Model.Count &gt; 0)
   {
       int j= 0;
       foreach (var item in Model)
       {
           @Html.DropDownListFor(m =&gt; item.Location, (IEnumerable&lt;SelectListItem&gt;)ViewBag.location, "", new { @class = "input-sm" })
           @Html.HiddenFor(m =&gt; item.SubscriberID)
           @Html.HiddenFor(m =&gt; item.MemberName)
           @Html.HiddenFor(m =&gt; item.DateOfBirth)
           @Html.HiddenFor(m =&gt; item.PCP)
           @Html.HiddenFor(m =&gt; item.IPA)
           @Html.HiddenFor(m =&gt; item.App)
           @Html.HiddenFor(m =&gt; item.RemainingOpportunities)
  </code></pre>
  
  <p>Produces this, there is no index on the name/ID:</p>
  
  <pre><code>&lt;input id="item_SubscriberID" name="item.SubscriberID" type="hidden" value="123456" /&gt;
  &lt;input id="item_MemberName" name="item.MemberName" type="hidden" value="FULL NAME" /&gt;
  &lt;input id="item_DateOfBirth" name="item.DateOfBirth" type="hidden" value="1/15/1900" /&gt;
  &lt;input id="item_PCP" name="item.PCP" type="hidden" value="NAME" /&gt;
  &lt;input id="item_IPA" name="item.IPA" type="hidden" value="Y" /&gt;
  &lt;input id="item_App" name="item.App" type="hidden" value="XXX" /&gt;
  &lt;input id="item_RemainingOpportunities" name="item.RemainingOpportunities" type="hidden" value="0" /&gt;
  </code></pre>
  
  <p>It doesn't seem correct that there is no index.  What am I missing??  I have been working on this for hours and I am starting to get a little cross-eyed over this.</p>