title: .NET MVC Post form only receiving first iteration of foreach loop
body: <p>I am working on my first .NET MVC project and I am to a point where I want my form to iterate through a loop of exercises in a workout (can be any number of exercises).  I have a foreach loop, and everything seems to work out ok, but when I check the database, it took only the entries for the first exercise, and applies that to all the entries.  I am not sure where I am messing up, or misunderstanding my issue.  Any advice in the right direction would be greatly appreciated.</p>
  
  <p>VIEW:</p>
  
  <pre><code>@model WorkoutGenerator.ViewModels.AddRecordViewModel
  
  &lt;h1&gt;Add Exercise Record&lt;/h1&gt;
  
  &lt;form asp-controller="Record" asp-action="Add" method="post"&gt;
      @foreach (var exercise in Model.Exercises)
      {
          &lt;h4&gt;@exercise.Exercise.Name&lt;/h4&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="@Model.Sets"&gt;&lt;/label&gt;
              &lt;input class="form-control" asp-for="@Model.Sets" /&gt;
              &lt;span asp-validation-for="@Model.Sets"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="@Model.Reps"&gt;&lt;/label&gt;
              &lt;input class="form-control" asp-for="@Model.Reps" /&gt;
              &lt;span asp-validation-for="@Model.Reps"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="@Model.Weight"&gt;&lt;/label&gt;
              &lt;input class="form-control" asp-for="@Model.Weight" /&gt;
              &lt;span asp-validation-for="@Model.Weight"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;input type="hidden" name="ExerciseID" value="@exercise.ExerciseID" /&gt;
          &lt;input type="hidden" name="WorkoutID" value="@exercise.WorkoutID" /&gt;
          &lt;input type="hidden" name="OwnerID" value="@exercise.Exercise.OwnerId" /&gt;
      }
  
      &lt;input type="submit" value="Add Exercise Record" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>VIEWMODEL:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using WorkoutGenerator.Models;
  
  namespace WorkoutGenerator.ViewModels
  {
      public class AddRecordViewModel
      {
          [Required(ErrorMessage = "Please enter number of sets")]
          public string Sets { get; set; }
  
          [Required(ErrorMessage = "Please enter number of reps")]
          public string Reps { get; set; }
  
          [Required(ErrorMessage = "Please enter amount of weight")]
          public string Weight { get; set; }
  
          //Date Created
          public DateTime DateCreated { get; set; }
          //Link to user
          public string OwnerId { get; set; }
          //Link to Exercise
          public int ExerciseID { get; set; }
          //Link to Workout
          public int WorkoutID { get; set; }
  
          public IList&lt;ExerciseWorkout&gt; Exercises { get; set; }
          public Workout Workout { get; set; }
  
          public AddRecordViewModel() { }
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code> public IActionResult Add(int id)
          {//Create form for each exercise to have sets reps and weight to submit
              //!!!!!!!!!!!!!!TAKEN FROM WORKOUT CONTROLLER!!!!!!!!!  MAY NEED CHANGING!!!!!!!!!!!!!!!!
              string user = User.Identity.Name;
              ApplicationUser userLoggedIn = context.Users.Single(c =&gt; c.UserName == user);
              List&lt;ExerciseWorkout&gt; exercises = context
                  .ExerciseWorkouts
                  .Include(item =&gt; item.Exercise)
                  .Where(cm =&gt; cm.WorkoutID == id &amp;&amp; cm.Workout.OwnerId == userLoggedIn.Id)//cm.Workout.OwnerId == userLoggedIn.Id returns list of owner specific workouts
                  .ToList();
  
              Workout workout = context.Workouts.Single(m =&gt; m.WorkoutID == id);
  
              AddRecordViewModel viewModel = new AddRecordViewModel
              {
                  Workout = workout,
                  Exercises = exercises
              };
  
              return View(viewModel);
          }
  
          [HttpPost]
          public IActionResult Add(AddRecordViewModel addRecordViewModel, int id)
          {//Create records of exercise sets reps and weights to be added to database.
              if (ModelState.IsValid)
              {
                  string user = User.Identity.Name;
                  ApplicationUser userLoggedIn = context.Users.Single(c =&gt; c.UserName == user);
                  //exercises hopefully returns list of exercises from 'int id' parameter,
                  //which can then be used to iterate over each exercise put into record table
                  List&lt;ExerciseWorkout&gt; exercises = context
                  .ExerciseWorkouts
                  .Include(item =&gt; item.Exercise)
                  .Where(cm =&gt; cm.WorkoutID == id &amp;&amp; cm.Workout.OwnerId == userLoggedIn.Id)
                  .ToList();
                  foreach (var exercise in exercises)
                  {
                      Record newRecord = new Record
                      {
                          Sets = addRecordViewModel.Sets,
                          Reps = addRecordViewModel.Reps,
                          Weight = addRecordViewModel.Weight,
                          DateCreated = DateTime.Now,//TODO Make this show only day not time of day
                          OwnerId = userLoggedIn.Id,//TODO Not Sure if creation of newRecord is correct.
                          WorkoutID = addRecordViewModel.WorkoutID,
                          FK_ExerciseID = addRecordViewModel.ExerciseID//TODO ExerciseID not entering into table.
                      };
                      context.Records.Add(newRecord);
                      context.SaveChanges();
                  }
                  return Redirect("/Record/Index");
              }
              else
              {
                  return View(addRecordViewModel);
              }
          }
  </code></pre>