public ViewResult Create() { var availableShoes = GetAvailableShoeList(); var workout = new EditWorkoutViewModel { WorkoutDate = DateTime.Today, AvailableShoes = availableShoes }; return View(workout); }
public ActionResult Create(EditWorkoutViewModel workoutViewModel) { if (ModelState.IsValid) { var workout = MapEditViewModelToWorkout(workoutViewModel); _dbContext.Workouts.Add(workout); _dbContext.SaveChanges(); return RedirectToAction("Index"); } return View(workoutViewModel); }
private Workout MapEditViewModelToWorkout(EditWorkoutViewModel workoutViewModel) { var shoeUsed = _dbContext.GetShoe(this.GetUserId(), workoutViewModel.ShoeUsedId); var workout = new Workout { UserId = this.GetUserId(), CreatedAt = DateTime.Now, Distance = workoutViewModel.Distance, Name = workoutViewModel.Name, ShoeUsed = shoeUsed, WorkoutDate = workoutViewModel.WorkoutDate, ImportedAt = DateTime.Now, }; return workout; }
private EditWorkoutViewModel MapWorkoutToEditWorkoutViewModel(Workout workout) { var shoeUsedId = workout.ShoeUsed == null ? (long?) null : workout.ShoeUsed.ShoeId; var availableShoes = GetAvailableShoeList(); var viewModel = new EditWorkoutViewModel { WorkoutId = workout.WorkoutId, WorkoutDate = workout.WorkoutDate, Distance = workout.Distance, ShoeUsedId = shoeUsedId, Name = workout.Name, AvailableShoes = availableShoes }; return viewModel; }
public ActionResult Edit(EditWorkoutViewModel workoutViewModel) { if (ModelState.IsValid) { var workout = _dbContext.GetWorkout(this.GetUserId(), workoutViewModel.WorkoutId); workout.Name = workoutViewModel.Name; workout.WorkoutDate = workoutViewModel.WorkoutDate; workout.Distance = workoutViewModel.Distance; var shoeUsed = _dbContext.GetShoe(this.GetUserId(), workoutViewModel.ShoeUsedId); workout.ShoeUsed = shoeUsed; _dbContext.SaveChanges(); return RedirectToAction("Index"); } return View(workoutViewModel); }