예제 #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");
        }
예제 #2
0
파일: QuizEntry.cs 프로젝트: psampaio/Quiz
 public Answer AddAnswer(string answerText, bool isCorrect = false)
 {
     var answer = new Answer(answerText, isCorrect);
     answers.Add(answer);
     return answer;
 }
예제 #3
0
 public void Add(Answer answer)
 {
     Answers.Add(answer);
 }