public ActionResult Create(Thread entity, FormCollection collection) { var session = (AdminLogin)Session[CommonConstants.USER_SESSION]; if (session.id_permission == 0) { return(View("Error")); } ViewBag.AdminName = session.name; if (ModelState.IsValid) { var dao = new ThreadDao(); var dao1 = new ThematicDao(); var dao2 = new QuestionDao(); var dao3 = new ExamDao(); int id_subject = Convert.ToInt32(collection["id_subject"]); int total_exam = Convert.ToInt32(collection["total_exam"]); int id_thread = dao.Insert(entity); if (id_thread > 0) { //tạo bộ câu hỏi cho đề thi List <ThematicModel> list_thematic = dao1.GetThematic(id_subject); List <int> idExamOfThread = new List <int>(); for (int i = 1; i <= total_exam; i++) { int id_exam = dao3.Insert(id_thread); idExamOfThread.Add(id_exam); } foreach (ThematicModel thematic in list_thematic) { //lấy danh sách câu hỏi trắc nghiệm ngẫu nhiên int quest_of_thematic = Convert.ToInt32(collection["unit-" + thematic.id_thematic]); List <Question> list_question = dao2.GetQuestionsByThematic(thematic.id_thematic, quest_of_thematic); foreach (Question item in list_question) { foreach (int id_exam in idExamOfThread) { dao2.AddQuestionsToExam(id_exam, item.id_question); } } //lấy danh sách câu hỏi tự luận ngẫu nhiên quest_of_thematic = Convert.ToInt32(collection["unit-essay-" + thematic.id_thematic]); List <Question> list_essay = dao2.GetEssayByThematic(thematic.id_thematic, quest_of_thematic); foreach (Question item in list_essay) { foreach (int id_exam in idExamOfThread) { dao2.AddEssaysToExam(item, id_exam); } } } //để thông báo thêm thành công SetNotice("Hệ thống đã thêm thành công.", "success"); return(RedirectToAction("Create")); } else { ModelState.AddModelError("", "Thêm kì thi không thành công."); } } var dao4 = new SubjectDao(); ViewBag.ListSubject = dao4.ListAll(); SetViewBag(); return(View()); }