public ActionResult Index() { _viewMessageLogic.SetViewMessageIfExist(); var importModel = new ImportDataModel(); ViewBag.GradeId = new SelectList(_gradeRepository.Query(x => x), "Id", "Name", 0); ViewBag.Year = new SelectList(_academicYearRepository.Filter(x => x.IsActive).Select(x => x.Year).Distinct().ToList()); ViewBag.Section = new SelectList(new List<string> { "A", "B", "C" }, "A"); return View(importModel); }
public ActionResult Index(ImportDataModel importModel) { var validImageTypes = new [] { "application/vnd.ms-excel" }; bool errorExcel; if (importModel.UploadFile != null && importModel.UploadFile.ContentLength > 0) errorExcel = !validImageTypes.Contains(importModel.UploadFile.ContentType); else errorExcel = true; if(errorExcel) ModelState.AddModelError("UploadFile", "Por favor seleccione un archivo de Excel"); var academicYear = _academicYearRepository.GetByFields(importModel.Year, importModel.GradeImport, importModel.Section); if (academicYear == null) ModelState.AddModelError("Year", "No existe ese año academico"); ViewBag.GradeId = new SelectList(_gradeRepository.Query(x => x), "Id", "Name", 0); if (!ModelState.IsValid) { return View(importModel); } var myDataSet = _importDataRepository.GetDataSetFromExcelFile(importModel.UploadFile); try { _importDataRepository.Import(myDataSet, academicYear); } catch(Exception ex) { _viewMessageLogic.SetNewMessage("Error!", ex.Message, ViewMessageType.ErrorMessage); return RedirectToAction("Index"); } const string title = "Importación de Datos Correcta"; var content = string.Format("Se importaron datos para el año: {0}, grado: {1} y sección: {2}" , importModel.Year // 0 , importModel.GradeImport // 1 , importModel.Section // 2 ); _viewMessageLogic.SetNewMessage(title, content, ViewMessageType.InformationMessage); return RedirectToAction("Index"); }