public ActionResult Create(TN_Question model) { if (ModelState.IsValid) { if (string.IsNullOrEmpty(model.Content)) { ModelState.AddModelError("", "Bạn phải nhập vào nội dung câu hỏi"); } else { var db = DB.GetContext(); db.TN_Question.AddObject(model); db.SaveChanges(); return RedirectToAction("Edit", new { id = model.ID }); } } return View(model); }
/// <summary> /// Deprecated Method for adding a new object to the TN_Question EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToTN_Question(TN_Question tN_Question) { base.AddObject("TN_Question", tN_Question); }
/// <summary> /// Create a new TN_Question object. /// </summary> /// <param name="id">Initial value of the ID property.</param> /// <param name="content">Initial value of the Content property.</param> /// <param name="isMultiChoose">Initial value of the IsMultiChoose property.</param> /// <param name="type">Initial value of the Type property.</param> public static TN_Question CreateTN_Question(global::System.Int32 id, global::System.String content, global::System.Boolean isMultiChoose, global::System.String type) { TN_Question tN_Question = new TN_Question(); tN_Question.ID = id; tN_Question.Content = content; tN_Question.IsMultiChoose = isMultiChoose; tN_Question.Type = type; return tN_Question; }
public ActionResult CreateQuestionFromExcelFile(int examID, HttpPostedFileBase file) { try { if (file != null) { var fileUtil = new FileUtil(); string folderUpload = @"D:\FileUploads\"; if (!fileUtil.CheckDirectoryExists(folderUpload)) { fileUtil.CreateDirectory(folderUpload); } string fileName = "nganhangcauhoi" + file.FileName.Substring(file.FileName.LastIndexOf(".")); file.SaveAs(folderUpload + fileName); // đọc file excel và lưu vào csdl //Stream streamDictionary = GetResourceFileStream(folderUpload + fileName); using (var document = SpreadsheetDocument.Open(folderUpload + fileName, false)) { var workbookPart = document.WorkbookPart; var workbook = workbookPart.Workbook; var sheets = workbook.Descendants<Sheet>(); foreach (var sheet in sheets) { if (sheet.SheetId.HasValue && sheet.SheetId.ToString().Equals("1")) { #region Read excel var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id); var sharedStringPart = workbookPart.SharedStringTablePart; var values = sharedStringPart.SharedStringTable.Elements<SharedStringItem>().ToArray(); var cells = worksheetPart.Worksheet.Descendants<Cell>(); var rows = worksheetPart.Worksheet.Descendants<Row>(); TN_Question questionTemp = new TN_Question(); int countDataNull = 0; bool flag = false; foreach (Row row in rows) { var question = new TN_Question(); var answer = new TN_Answer(); foreach (Cell c in row.Elements<Cell>()) { string value = string.Empty; if ((c.DataType != null) && (c.DataType == CellValues.SharedString)) { int ssid = int.Parse(c.CellValue.Text); value = sharedStringPart.SharedStringTable.ChildElements[ssid].InnerText; } else if (c.CellValue != null) { value = c.CellValue.Text; } // xử lý ban đầu value = value.TrimEnd('%'); if (c.CellReference == "A" + row.RowIndex) { if (!string.IsNullOrEmpty(value)) { question.Content = value; answer.Content = value; countDataNull = 0; } else { countDataNull++; break; } } if (c.CellReference == "B" + row.RowIndex) { if (!string.IsNullOrEmpty(value)) { answer.IsCorrect = true; } else { answer.IsCorrect = false; } } if (c.CellReference == "C" + row.RowIndex && !string.IsNullOrEmpty(value)) { if (!string.IsNullOrEmpty(value)) { question.Type = value; flag = true; } } } if (countDataNull == 0) { if (flag) { question.IsMultiChoose = false; db.TN_Question.AddObject(question); questionTemp = question; var questionExam = new TN_ExamQuestion() { ExamID = examID, QuestionID = question.ID }; db.TN_ExamQuestion.AddObject(questionExam); flag = false; } else { answer.QuestionID = questionTemp.ID; db.TN_Answer.AddObject(answer); } db.SaveChanges(); } } #endregion break; } } } ViewBag.Success = true; } } catch (Exception ex) { } return View(new List<TN_Exam>()); }
public ActionResult Edit(int id, TN_Question model) { var db = DB.GetContext(); var question = db.TN_Question.SingleOrDefault(m => m.ID == id); question.Content = model.Content; question.IsMultiChoose = model.IsMultiChoose; db.SaveChanges(); return RedirectToAction("Edit", new { id = id }); }