public ActionResult Index() { var listCategories = new List <Models.QuestionCategory>(); listCategories = questionCategorySevice.GetAll().ToList(); ViewBag.ListCategories = listCategories; return(View()); }
public ActionResult ImportExamPaper(HttpPostedFileBase excelfile) { if (excelfile == null) { Failure = "Please choose excel file to import exam paper"; return(RedirectToAction("ImportExamPaper")); } else { if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx")) { try { string path = Path.Combine(Server.MapPath("~/FileExcel/"), Guid.NewGuid().ToString() + Path.GetExtension(excelfile.FileName)); excelfile.SaveAs(path); Excel.Application application = new Excel.Application { Visible = true }; Excel.Workbook workbook = application.Workbooks.Open(path); Excel.Worksheet worksheet = workbook.Sheets[@"ExamPaper"]; Excel.Range range = worksheet.UsedRange; Models.ExamPaper examPaper = new Models.ExamPaper(); examPaper.Title = ((Excel.Range)range.Cells[3, 2]).Text; examPaper.Time = int.Parse(((Excel.Range)range.Cells[4, 2]).Text); if (((Excel.Range)range.Cells[5, 2]).Text == "Public") { examPaper.Status = true; } else if (((Excel.Range)range.Cells[5, 2]).Text == "Draff") { examPaper.Status = false; } else { Failure = "Exam paper status must be select from dropdown list"; return(RedirectToAction("ImportExamPaper")); } examPaper.IsActive = Boolean.Parse(((Excel.Range)range.Cells[6, 2]).Text); examPaper.CreatedBy = int.Parse(Session["Name"].ToString()); examPaper.CreatedDate = DateTime.Now; examPaper.ModifiedBy = int.Parse(Session["Name"].ToString()); examPaper.ModifiedDate = DateTime.Now; int examPaperId = examPaperService.Create(examPaper); var listQuestionCategory = questionCategorySevice.GetAll(); for (int row = 11; row <= range.Rows.Count; row++) { int level = 0; if (((Excel.Range)range.Cells[row, 2]).Text == "Hard") { level = 3; } else if (((Excel.Range)range.Cells[row, 2]).Text == "Normal") { level = 2; } else if (((Excel.Range)range.Cells[row, 2]).Text == "Easy") { level = 1; } else { Failure = "Question level must be select from dropdown list"; return(RedirectToAction("ImportExamPaper")); } int categoryId = 0; int k = 0; foreach (var item in listQuestionCategory) { if (((Excel.Range)range.Cells[row, 3]).Text == item.Name) { categoryId = item.CategoryID; k++; } } if (k == 0) { Failure = "Question category must be select from dropdown list"; return(RedirectToAction("ImportExamPaper")); } Models.Question question = new Models.Question { Content = ((Excel.Range)range.Cells[row, 1]).Text, CategoryID = categoryId, Level = level, IsActive = true, CreatedBy = int.Parse(Session["Name"].ToString()), CreatedDate = DateTime.Now, ModifiedBy = int.Parse(Session["Name"].ToString()), ModifiedDate = DateTime.Now }; int questionId = questionService.AddQuestion(question); Answer answer = new Answer(); int j = 5; for (int i = 4; i <= 13; i += 2) { string content = ((Excel.Range)range.Cells[row, i]).Text; if (content != "") { answer.AnswerContent = content; answer.IsCorrect = Boolean.Parse(((Excel.Range)range.Cells[row, j]).Text); answer.QuestionID = questionId; answerService.AddAnswer(answer); } else { continue; } j += 2; } examPaperQuestionService.InsertExamPaperQuestion(examPaperId, questionId); } } catch (Exception ex) { Failure = ex.Message; return(RedirectToAction("ImportExamPaper")); } Success = "Import exam paper successfully!"; return(RedirectToAction("ExamPapers")); } else { Failure = "Please choose excel file to import exam paper"; return(RedirectToAction("ImportExamPaper")); } } }