public ActionResult Edit(int? id, QuestionViewModel questionViewModel)
        {
            _connection.Open();
            var examId = _connection.Query<int>("SELECT TOP 1 ExamId FROM Questions WHERE QuestionId = @QuestionId", new { QuestionId = id }).Single();
            ViewBag.ExamId = examId;

            var moreThanOneAnswer = questionViewModel.Answers.Count(a => a.IsCorrect) != 1;
            if(moreThanOneAnswer)
                ModelState.AddModelError("OnlyOneAnswer","There must be one and only one correct answer");
            var anyBlankAnswers = questionViewModel.Answers.Any(a => string.IsNullOrEmpty(a.AnswerText));
            if(anyBlankAnswers)
                ModelState.AddModelError("BlankAnswers", "You must enter text for all answers");

            if (!ModelState.IsValid)
            {
                _connection.Close();
                return View(questionViewModel);
            }

            _connection.Execute("UPDATE Questions SET QuestionText = @QuestionText WHERE QuestionId = @QuestionId",
                                new {QuestionId = id, questionViewModel.QuestionText});
            foreach (var answer in questionViewModel.Answers)
            {
                _connection.Execute("UPDATE Answers SET AnswerText = @AnswerText, IsCorrect = @IsCorrect WHERE AnswerId = @AnswerId",
                    new { answer.AnswerText, answer.IsCorrect, answer.AnswerId });
            }
            _connection.Close();
            return RedirectToAction("Index", new { id = examId });
        }
        public ActionResult Create(int? id, QuestionViewModel questionViewModel)
        {
            ViewBag.ExamId = id;
            var moreThanOneAnswer = questionViewModel.Answers.Count(a => a.IsCorrect) != 1;
            if(moreThanOneAnswer)
                ModelState.AddModelError("OnlyOneAnswer","There must be one and only one correct answer");
            var anyBlankAnswers = questionViewModel.Answers.Any(a => string.IsNullOrEmpty(a.AnswerText));
            if(anyBlankAnswers)
                ModelState.AddModelError("BlankAnswers", "You must enter text for all answers");

            if (!ModelState.IsValid)
                return View(questionViewModel);

            _connection.Open();
            var questionId = _connection.Query<int>("INSERT INTO Questions (ExamId, QuestionText) VALUES (@ExamId, @QuestionText); SELECT CAST(SCOPE_IDENTITY() AS int);",
                new { ExamId = id, questionViewModel.QuestionText });
            for (var i = 0; i < 4;i++)
            {
                var answer = questionViewModel.Answers[i];
                _connection.Execute("INSERT INTO Answers (AnswerText, QuestionId, IsCorrect) VALUES (@AnswerText, @QuestionId, @IsCorrect)",
                    new { answer.AnswerText, QuestionId = questionId, answer.IsCorrect });
            }
            _connection.Close();
            return RedirectToAction("Index", new { id });
        }
 public ActionResult Edit(int? id)
 {
     _connection.Open();
     var questionViewModel = new QuestionViewModel();
     var question = _connection.Query<Question>("SELECT TOP 1 QuestionText, ExamId FROM Questions WHERE QuestionId = @QuestionId", new { QuestionId = id }).Single();
     ViewBag.ExamId = question.ExamId;
     questionViewModel.QuestionText = question.QuestionText;
     questionViewModel.Answers = _connection.Query<Answer>("SELECT AnswerId, AnswerText, IsCorrect FROM Answers WHERE QuestionId = @QuestionId", new { QuestionId = id }).ToList();
     _connection.Close();
     return View(questionViewModel);
 }
예제 #4
0
        public ViewResult Print(int? id)
        {
            _connection.Open();
            var printExamViewModel = new PrintExamViewModel();
            printExamViewModel.ExamKey = Guid.NewGuid();
            printExamViewModel.Exam = _connection.Query<Exam>("SELECT TOP 1 ExamId, ExamName FROM Exams WHERE ExamId = @ExamId", new { ExamId = id }).Single();

            var questions = _connection.Query<Question>("SELECT QuestionId, QuestionText FROM Questions WHERE ExamId = @ExamId ORDER BY NEWID()", new { ExamId = id });
            var questionNumber = 1;
            foreach (var question in questions)
            {
                var questionViewModel = new QuestionViewModel();
                questionViewModel.QuestionText = question.QuestionText;
                questionViewModel.Answers = _connection.Query<Answer>("SELECT AnswerId, AnswerText, IsCorrect FROM Answers WHERE QuestionId = @QuestionID ORDER BY NEWID()",
                    new {question.QuestionId }).ToList();
                var correctAnswer = questionViewModel.Answers.Single(a => a.IsCorrect);
                printExamViewModel.AnswerKey.Add(ConvertNumToLetter(questionViewModel.Answers.IndexOf(correctAnswer)));
                printExamViewModel.Questions.Add(questionViewModel);
                questionNumber++;
            }
            _connection.Close();
            return View(printExamViewModel);
        }