Accessing Properties of IEnumberable ViewModel Parameter in IActionResult 'POST' Method
I am currently working on my first .NET app, a workout tracker. I have implemented the exercises and workouts, but am having an issue with getting the records to enter into my database. I have a view model that Enumerates over itself, but when I go to use that as the parameter for my 'POST' method in my controller, I am not able to access the properties(sets, reps, weight...) of my view model. I am sure it is something simple, but I just can't seem to see what it might be. I have code posted below, and https://github.com/bdburns6389/WorkoutGenerator contains the entire app (under the OneToMany branch right now).
View: @using System.Collections.Generic; @model List<WorkoutGenerator.ViewModels.AddRecordViewModel>
<h1>Add Exercise Record</h1> <form asp-controller="Record" asp-action="Add" method="post"> @foreach (var exercise in Model) { <h4>@exercise.ExerciseID</h4> <div class="form-group"> <label asp-for="@exercise.Sets"></label> <input class="form-control" asp-for="@exercise.Sets" /> <span asp-validation-for="@exercise.Sets"></span> </div> <div class="form-group"> <label asp-for="@exercise.Reps"></label> <input class="form-control" asp-for="@exercise.Reps" /> <span asp-validation-for="@exercise.Reps"></span> </div> <div class="form-group"> <label asp-for="@exercise.Weight"></label> <input class="form-control" asp-for="@exercise.Weight" /> <span asp-validation-for="@exercise.Weight"></span> </div> <input type="hidden" name="ExerciseID" value="@exercise.ExerciseID" /> <input type="hidden" name="WorkoutID" value="@exercise.WorkoutID" /> <input type="hidden" name="OwnerID" value="@exercise.OwnerId" /> } <input type="submit" value="Add Exercise Record" /> </form>
ViewModel 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 List<ExerciseWorkout> Exercises { get; set; } public Workout Workout { get; set; } public AddRecordViewModel() { } } }
Controller:
[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 => 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<ExerciseWorkout> exercises = context .ExerciseWorkouts .Include(item => item.Exercise) .Where(cm => cm.WorkoutID == id && 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); } }
0 comments:
Post a Comment