Ejemplo n.º 1
0
        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");
        }
Ejemplo n.º 2
0
 public Answer AddAnswer(string answerText, bool isCorrect = false)
 {
     var answer = new Answer(answerText, isCorrect);
     answers.Add(answer);
     return answer;
 }
Ejemplo n.º 3
0
 public void Add(Answer answer)
 {
     Answers.Add(answer);
 }