public void Update(Question question) { var currentQuestion = Questions.Find(question.Id); Entry(currentQuestion).CurrentValues.SetValues(question); foreach (var answer in question.PossibleAnswers) { var currentAnswer = Answers.Find(answer.Id); Entry(currentAnswer).CurrentValues.SetValues(answer); } }
public ActionResult Create(CreateQuestionViewModel questionViewModel) { if (!ModelState.IsValid) return View(questionViewModel); var correctAnswer = new Answer { Text = questionViewModel.CorrectAnswer.Text }; var possibleAnswers = new List<Answer>(questionViewModel.FaultyAnswers.Select(a => new Answer { Text = a.Text })) { correctAnswer }; // Step 1 - sätt in alla svar possibleAnswers.ForEach(a => _quizContext.Add(a)); _quizContext.Save(); var question = new Question { CorrectAnswer = correctAnswer, PossibleAnswers = possibleAnswers, Text = questionViewModel.Text, UpdateStamp = DateTime.Now }; _quizContext.Add(question); try { _quizContext.Save(); } catch (Exception e) { return View(questionViewModel); } return RedirectToAction("Index"); }
public ActionResult Edit(CreateQuestionViewModel questionViewModel) { if (!ModelState.IsValid) return View("Edit", questionViewModel); var possibleAnswers = questionViewModel.FaultyAnswers .Select(a => new Answer { Id = a.Id, Text = a.Text }).ToList(); possibleAnswers.Add( new Answer { Id = questionViewModel.CorrectAnswer.Id, Text = questionViewModel.CorrectAnswer.Text }); var question = new Question { CorrectAnswer = new Answer { Id = questionViewModel.CorrectAnswer.Id, Text = questionViewModel.CorrectAnswer.Text }, Id = questionViewModel.Id, PossibleAnswers = possibleAnswers, Text = questionViewModel.Text, UpdateStamp = DateTime.Now }; _quizContext.Update(question); try { _quizContext.Save(); } catch (Exception e) { return View("Edit", questionViewModel); } return RedirectToAction("Index"); }
public QuizEntry(string question, Level level) { Level = level; Question = new Question(question); answers = new List<Answer>(); }
public void Add(Question question) { Questions.Add(question); }