public ExamContentEditViewModel(SATExamContent model) { ExamContent = model; using (var db = new SATEntities()) { ContentLines = ExamContent.SATContentLines.ToList(); //ContentLines = db.SATExamContents.Include("SATContentLine").Where(e => e.ID == model.ID).FirstOrDefault().SATContentLines.ToList(); } }
public ActionResult DeleteConfirmed(int id) { SATExamContent sATExamContent = db.SATExamContents.Find(id); var lines = db.SATContentLines.Where(l => l.ExamContentID == id).ToList(); db.SATContentLines.RemoveRange(lines); db.SATExamContents.Remove(sATExamContent); db.SaveChanges(); return(RedirectToAction("Index")); }
// GET: ttn_content/ExamContent/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } SATExamContent sATExamContent = db.SATExamContents.Find(id); if (sATExamContent == null) { return(HttpNotFound()); } return(View(sATExamContent)); }
// GET: ttn_content/ExamContent/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } SATExamContent sATExamContent = db.SATExamContents.Find(id); if (sATExamContent == null) { return(HttpNotFound()); } ViewBag.ExamFormID = new SelectList(db.SATExamForms.Where(ef => ef.Status == true), "ID", "Title", sATExamContent.ExamFormID); var listUnSetExamContentIDQuestion = new SelectList(db.SATQuestions.Where(q => q.ExamContentID == null && q.SATWritingUnderlineTexts.Any(u => u.QuestionID == q.ID) == false).ToList(), "ID", "Title"); ViewBag.ListQuestion = listUnSetExamContentIDQuestion; var viewModel = new ExamContentEditViewModel(sATExamContent); return(View(viewModel)); }
public ActionResult Create([Bind(Include = "ID,ExamContentIndex,AttachImage,Contents,Status,ExamFormID,Title,Intro")] SATExamContent sATExamContent, FormCollection collection) { var contentLines = collection.GetValues("inputLine") != null?collection.GetValues("inputLine").ToList() : new List <string>(); var questionToAdd = collection.GetValues("questionToAdd") != null?collection.GetValues("questionToAdd").ToList() : new List <string>(); if (ModelState.IsValid) { using (var dbTransaction = db.Database.BeginTransaction()) { try { //add exam content sATExamContent.rowguid = Guid.NewGuid(); db.SATExamContents.Add(sATExamContent); if (db.SaveChanges() > 0) { //add conten lines var newId = db.SATExamContents.FirstOrDefault(e => e.rowguid == sATExamContent.rowguid); if (newId != null) { var index = 1; foreach (var item in contentLines) { var line = new SATContentLine(); line.LineIndex = index; index = index++; line.LineText = item; line.ExamContentID = newId.ID; line.rowguid = Guid.NewGuid(); db.SATContentLines.Add(line); db.SaveChanges(); } } //update question's ExamContentID foreach (var item in questionToAdd) { if (!string.IsNullOrEmpty(item)) { var questionID = int.Parse(item); var question = db.SATQuestions.Find(questionID); question.ExamContentID = newId.ID; db.Entry(question).State = EntityState.Modified; db.SaveChanges(); } } } // db.SaveChanges(); dbTransaction.Commit(); return(RedirectToAction("Index")); } catch (Exception) { dbTransaction.Rollback(); ViewBag.ExamFormID = new SelectList(db.SATExamForms.Where(ef => ef.Status == true), "ID", "Title", sATExamContent.ExamFormID); ViewBag.ContentLines = contentLines; var listUnSetExamContentIDQuestion = new SelectList(db.SATQuestions.Where(q => q.ExamContentID == null && q.SATWritingUnderlineTexts.Any(u => u.QuestionID == q.ID) == false).ToList(), "ID", "Title"); ViewBag.ListQuestion = listUnSetExamContentIDQuestion; ViewBag.ListInputHidden = questionToAdd; return(View(sATExamContent)); } } } ViewBag.ExamFormID = new SelectList(db.SATExamForms.Where(ef => ef.Status == true), "ID", "Title", sATExamContent.ExamFormID); ViewBag.ContentLines = contentLines; return(View(sATExamContent)); }