Example #1
0
 public ActionResult EditQuiz(int?id)
 {
     using (var _context = new TRACNGHIEM_ONLINEEntities())
     {
         var q = _context.Quizs.SingleOrDefault(n => n.ID_Quiz == id);
         return(View(q));
     }
 }
Example #2
0
 public ActionResult Search(string sTuKhoa)
 {
     using (var _context = new TRACNGHIEM_ONLINEEntities())
     {
         var lstQuiz = _context.Quizs.Where(x => x.Title.Contains(sTuKhoa)).ToList();
         return(PartialView(lstQuiz.OrderBy(n => n.ID_Quiz)));
     }
 }
 public ActionResult AddQuiz(QuizViewModel quiz, IEnumerable <QuestionViewModel> modelQuestion, IEnumerable <AnswerViewModel> modelAnswer)
 {
     using (var _context = new TRACNGHIEM_ONLINEEntities())
     {
         Quiz            q           = new Quiz();
         List <Question> lstQuestion = new List <Question>();
         List <Answer>   lstAnswer   = new List <Answer>();
         var             id_Quiz     = _context.Quizs.ToList().Count();
         if (id_Quiz == 0)
         {
             q.ID_Quiz = 1;
         }
         else
         {
             q.ID_Quiz = id_Quiz + 1;
         }
         q.Title       = quiz.Title;
         q.Keys        = quiz.Keys;
         q.Time_Start  = quiz.Time_Start;
         q.Time_Finish = quiz.Time_Finish;
         q.Time        = quiz.Time;
         //q.Creator = quiz.Creator;
         q.Creator      = 1;
         q.Allow_Attemp = 1;
         _context.Quizs.Add(q);
         _context.SaveChanges();
         //Question
         foreach (var item in modelQuestion)
         {
             if (item.ID_Question != 0)
             {
                 Question i = new Question();
                 i.ID_Quiz             = q.ID_Quiz;
                 i.ID_Question         = item.ID_Question;
                 i.Content_of_Question = item.Content_of_Question;
                 i.Type_of_Question    = item.Type_of_Question;
                 i.Score = item.Score;
                 lstQuestion.Add(i);
             }
         }
         _context.Questions.AddRange(lstQuestion);
         _context.SaveChanges();
         //Answer
         foreach (var item in modelAnswer)
         {
             Answer a = new Answer();
             a.ID_Quiz        = q.ID_Quiz;
             a.ID_Question    = item.ID;
             a.Answer1        = item.Answer;
             a.Correct_Answer = item.Correct_Answer == true ? true : false;
             lstAnswer.Add(a);
         }
         _context.Answers.AddRange(lstAnswer);
         _context.SaveChanges();
     }
     return(View());
 }
Example #4
0
 public ActionResult DeleteQuiz(int?id)
 {
     using (var _context = new TRACNGHIEM_ONLINEEntities())
     {
         _context.Quizs.Remove(_context.Quizs.SingleOrDefault(n => n.ID_Quiz == id));
         _context.SaveChanges();
     }
     return(RedirectToAction("Quiz"));
 }
Example #5
0
        public ActionResult Quiz()
        {
            IEnumerable <Quiz> lstQuiz;

            using (var _context = new TRACNGHIEM_ONLINEEntities())
            {
                lstQuiz = _context.Quizs.ToList();
            }
            return(View(lstQuiz));
        }
Example #6
0
 public ActionResult EditQuestion(int?id)
 {
     using (var _context = new TRACNGHIEM_ONLINEEntities())
     {
         var q = _context.Questions.SingleOrDefault(n => n.ID_Question == id);
         var a = _context.Answers.ToList().Where(n => n.ID_Question == id);
         ViewBag.Answers = a;
         ViewBag.Id      = q.ID_Quiz;
         return(View(q));
     }
 }
Example #7
0
        public ActionResult UpdateQuiz(Quiz q)
        {
            Quiz update = new Quiz();

            using (var _context = new TRACNGHIEM_ONLINEEntities())
            {
                update = q;
                _context.Quizs.AddOrUpdate(update);
                _context.SaveChanges();
            }
            return(RedirectToAction("Quiz"));
        }
Example #8
0
        public ActionResult Question(int?id)
        {
            IEnumerable <Question> lstQuestion;
            IEnumerable <Answer>   lstAnswer;

            using (var _context = new TRACNGHIEM_ONLINEEntities())
            {
                lstQuestion = _context.Questions.ToList().Where(n => n.ID_Quiz == id);
                lstAnswer   = _context.Answers.ToList().Where(n => n.ID_Quiz == id);
            }
            ViewBag.Answers = lstAnswer;
            return(View(lstQuestion));
        }
Example #9
0
        public ActionResult UpdateQuestion(Question q, IEnumerable <AnswerViewModel> a)
        {
            Question      qupdate = new Question();
            List <Answer> aupdate = new List <Answer>();

            foreach (var item in a)
            {
                Answer i = new Answer();
                i.ID_Quiz        = q.ID_Quiz;
                i.ID_Question    = item.ID;
                i.Answer1        = item.Answer;
                i.Correct_Answer = item.Correct_Answer == true ? true : false;
                aupdate.Add(i);
            }
            using (var _context = new TRACNGHIEM_ONLINEEntities())
            {
                qupdate = q;
                _context.Questions.AddOrUpdate(qupdate);
                _context.Answers.RemoveRange(_context.Answers.ToList().Where(n => n.ID_Quiz == q.ID_Quiz && n.ID_Question == q.ID_Question));
                _context.Answers.AddRange(aupdate);
                _context.SaveChanges();
            }
            return(RedirectToAction("Question", new { id = q.ID_Quiz }));
        }