示例#1
0
        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);
            }
        }
示例#2
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");
        }
示例#3
0
        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");
        }
示例#4
0
 public QuizEntry(string question, Level level)
 {
     Level = level;
     Question = new Question(question);
     answers = new List<Answer>();
 }
示例#5
0
 public void Add(Question question)
 {
     Questions.Add(question);
 }