protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         db.Dispose();
     }
     base.Dispose(disposing);
 }
Пример #2
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    dataContext.Dispose();
                    disposed = true;
                }
            }

            disposed = false;
        }
Пример #3
0
        public ActionResult Create(QuestionCreateViewModel model, List <SATAnswer> Answers, string btnSave)
        {
            var db       = new SATEntities();
            var listType = new SelectList(db.SATTypes.ToList(), "TypeID", "TypeName");

            ViewBag.Type = listType;
            List <SelectListItem> listCalc = UseCalcSelectList();

            ViewBag.UseCalc = new SelectList(listCalc, "Value", "Text");
            var listExamContent = new ExamContentSelectList().GetExamContentSelectList();

            ViewBag.ExamContentID = listExamContent;
            db.Dispose();
            var hasInput = true;
            var textType = new SelectListItem()
            {
                Value = "1", Text = "Nhập vào"
            };
            var choiceType = new SelectListItem()
            {
                Value = "0", Text = "Chọn câu", Disabled = hasInput ? true : false
            };

            ViewBag.AnswerType = new SelectList(new List <SelectListItem>()
            {
                choiceType, textType
            }, "Value", "Text", hasInput ? "1" : "0");
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.FormValue = JsonConvert.SerializeObject(model);
                    if (Session["IsInputAnswer"] != null)
                    {
                        ViewBag.IsInputAnswer = (bool)Session["IsInputAnswer"];
                    }

                    return(View("CreateWithAnswer", model));
                }
                using (var dbContext = new SATEntities())
                {
                    var questionModel = new SATQuestion();
                    questionModel.ExamContentID   = model.ExamContentID;
                    questionModel.HasInputAnswer  = model.HasInputAnswer;
                    questionModel.QuestionContent = model.QuestionContent;
                    questionModel.QuestionNo      = model.QuestionNo;
                    questionModel.Status          = btnSave == "Tạo mới & Đăng" ? true : false;
                    questionModel.Title           = model.Title;
                    questionModel.TypeID          = model.TypeID;


                    dbContext.SATQuestions.Add(questionModel);
                    dbContext.SaveChanges();

                    var lastest = dbContext.SATQuestions.Max(q => q.ID);
                    foreach (var item in Answers)
                    {
                        item.QuestionID = lastest;
                    }
                    dbContext.SATAnswers.AddRange(Answers);
                    dbContext.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View("CreateWithAnswer", model));
            }
        }