public async Task <ActionResult> CreateQuiz(IFormCollection collection, AddQuiz_VM vm) { try { if (ModelState.IsValid) { if (vm.ImgUrl == null) { vm.ImgUrl = "https://wallpaperaccess.com/full/2384075.jpg"; } Quiz quiz = new Quiz() { SubjectId = vm.SubjectId, QuizName = vm.QuizName, QuizID = Guid.NewGuid(), ImgUrl = vm.ImgUrl, Difficulty = vm.Difficulty, Description = vm.Description }; Quiz result = await quizRepo.AddQuiz(quiz); if (result == null) { throw new Exception("Invalid Entry"); } return(RedirectToAction(nameof(Quizzes))); } return(View(vm)); } catch (Exception ex) { Debug.WriteLine("unable to create" + ex.Message); ModelState.AddModelError("", "Create mislukt." + ex.Message); return(View(vm)); } }
public async Task <ActionResult> CreateQuizAsync() { try { AddQuiz_VM addQuiz = new AddQuiz_VM() { Subjects = new List <Subject>() }; var subjects = await subjectRepo.GetSubjectsAsync(); foreach (var item in subjects) { addQuiz.Subjects.Add(item); } return(View(addQuiz)); } catch (Exception ex) { throw ex; } }
public async Task <ActionResult> DetailQuizAsync(string id) { if (id == null) { return(RedirectToAction("Quizzes")); } var quiz = await quizRepo.GetQuizByIdAsync(Guid.Parse(id)); var subject = await subjectRepo.GetsubjectById(quiz.SubjectId); AddQuiz_VM vm = new AddQuiz_VM() { QuizID = quiz.QuizID, Description = quiz.Description, Difficulty = quiz.Difficulty, ImgUrl = quiz.ImgUrl, QuizName = quiz.QuizName, SubjectName = subject.SubjectName }; return(View(vm)); }
public async Task <ActionResult> EditQuizAsync(Guid Id) { try { if (Id == null) { return(RedirectToAction("Quizzes")); } Quiz quiz = await quizRepo.GetQuizByIdAsync(Id); var subjects = await subjectRepo.GetSubjectsAsync(); AddQuiz_VM vm = new AddQuiz_VM() { ImgUrl = quiz.ImgUrl, QuizID = quiz.QuizID, Description = quiz.Description, Difficulty = quiz.Difficulty, QuizName = quiz.QuizName, SubjectId = quiz.SubjectId, Subjects = new List <Subject>(), }; foreach (var item in subjects) { vm.Subjects.Add(item); } return(View(vm)); } catch (Exception ex) { Debug.WriteLine($"update error. {ex.Message}"); ModelState.AddModelError("", "Update actie mislukt." + ex.InnerException.Message); return(RedirectToAction(nameof(Quizzes))); } }
public async Task <ActionResult> EditQuizAsync(Guid Id, IFormCollection collection, AddQuiz_VM vm) { try { // TODO: Add update logic here if (Id == null) { return(RedirectToAction("Quizzes")); } //default image if (vm.ImgUrl == null) { vm.ImgUrl = "https://wallpaperaccess.com/full/2384075.jpg"; } Quiz quiz = new Quiz() { SubjectId = vm.SubjectId, Description = vm.Description, Difficulty = vm.Difficulty, ImgUrl = vm.ImgUrl, QuizID = vm.QuizID, QuizName = vm.QuizName }; var result = await quizRepo.Update(quiz); if (result == null) { return(Redirect("/Error/400")); } return(RedirectToAction(nameof(Quizzes))); } catch (Exception ex) { Debug.WriteLine($"update error. {ex.Message}"); ModelState.AddModelError("", "Update actie mislukt." + ex.InnerException.Message); return(View(vm)); } }