Exemplo n.º 1
0
        public ActionResult AddQuest(HttpPostedFileBase file, int id_subject, int id_thematic)
        {
            try
            {
                var dao     = new QuestionDao();
                var session = (AdminLogin)Session[CommonConstants.USER_SESSION];
                if (session.id_permission == 2)
                {
                    return(View("Error"));
                }
                ViewBag.AdminName = session.name;

                var ad = new ImportExport();
                HttpPostedFileBase upload = Request.Files["file"];
                string             path   = Path.Combine(Server.MapPath("~/App_Data/uploads"), "cauhoi.xlsx");
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                }
                var data = ad.ReadFromExcelfile(path, "");

                int id = dao.InsertList(data, id_thematic);
                if (id > 0)
                {
                    SetViewBag(id_subject);
                    //để thông báo thêm thành công
                    SetNotice("Hệ thống đã thêm thành công " + id.ToString() + " câu hỏi", "success");
                    return(RedirectToAction("QDetail", new { id = id_subject }));
                }
                else
                {
                    var dao2 = new SubjectDao().ViewDetail(id_subject);
                    TempData["subject_name"] = dao2.subject_name;
                    TempData["id_subject"]   = id_subject;
                    SetViewBag(id_subject);
                    ModelState.AddModelError("", "Thêm câu hỏi không thành công.");
                }
            }
            catch
            {
                var dao2 = new SubjectDao().ViewDetail(id_subject);
                TempData["subject_name"] = dao2.subject_name;
                TempData["id_subject"]   = id_subject;
                SetViewBag(id_subject);
                ModelState.AddModelError("", "Thêm câu hỏi không thành công. Vui lòng kiểm tra file!");
            }

            var dao3 = new SubjectDao().ViewDetail(id_subject);

            TempData["subject_name"] = dao3.subject_name;
            TempData["id_subject"]   = id_subject;
            SetViewBag(id_subject);
            return(View());
        }