public ActionResult EditStepExercise(int?roadStepId, int?id) { var model = new ManageStepExerciseViewModel(); if (id != null) { var stepExercise = Db.StepExercises .FirstOrDefault(p => p.Id == id); if (stepExercise != null) { model.Id = stepExercise.Id; model.Name = stepExercise.Name; model.Description = stepExercise.Description; if (roadStepId != null) { model.RoadStepId = roadStepId; } } } return(View(model)); }
public ActionResult EditStepExercise(ManageStepExerciseViewModel model) { if (!ModelState.IsValid) { return(View(model)); } StepExercise stepExercise = null; if (model.Id != null) { stepExercise = Db.StepExercises.FirstOrDefault(p => p.Id == model.Id); if (stepExercise != null) { stepExercise.Name = model.Name; stepExercise.Description = model.Description; } } else { var currentUserId = User.Identity.GetUserId(); RoadStep roadStep = null; if (model.RoadStepId != null) { roadStep = Db.RoadSteps.FirstOrDefault(p => p.Id == model.RoadStepId); } stepExercise = new StepExercise(model.Name, model.Description, currentUserId, roadStep); Db.StepExercises.Add(stepExercise); } Db.SaveChanges(); return(RedirectToAction("EditRoadStep", new { @id = model.RoadStepId })); }