First of all, apologies for the title; I wasn't sure of the best way to word this issue. Basically I'm creating my first web application using ASP.NET MVC 5 and am unsure how to handle the creation of new objects using form data and then associating them with another object.
The application I'm creating is a exercise/workout log. What I want to be able to do is to create a new workout, add records of exercises completed, and then save the workout entry to the database.
In order to do this I have created a Workout class, an Exercise class, and an ExerciseRecord class.
The Workout class has:
- date
- duration
- collection of ExerciseRecords objects
The ExerciseRecord class has:
- exercise sets
- exercise repetitions
- weight lifted
- an association to a single Exercise object
- an association to a single Workout object
When a user wishes to create a workout, they are directed to the create/edit view of my WorkoutController, and a new Workout object (say w1) is created with default values set for date and duration.
The view consists of a form where the user can enter a workout date, time, and details of an exercise record such as exercise name, reps, sets, and weight (displayed as a table).
What I want to happen is that when the user clicks to add a new exercise record, an ExerciseRecord object is using the details entered and is then added to the collection of ExerciseRecord objects associated with the current Workout (w1). Then, once all exercise records have been added, the user can save the Workout to the database.
The way I had approached this was to consider the newly created Workout as almost like a shopping cart, and when a new exercise record is added, it would be like adding a product to the cart. Saving the workout would be akin to checking out/completing an order.
My thinking was to have an exercise record row as a small form so that when a user clicks to add an exercise, the data is submitted and then added to the current Workout object and the user is redirected back to the create/view page where the newly added ExerciseRecord detail will appear.
I was thinking that I could pass the currently 'open' Workout object around within the TempData but I'm not sure if I'm going about all of this in the right way.
Any advice is much appreciated.