public async Task <IActionResult> Edit(int id, [Bind("Id,LakeId,Year,LakePart,Mineralization,TotalHardness,DissOxygWater,PercentOxygWater,pH,OrganicSubstances")] GeneralHydrochemicalIndicator generalHydrochemicalIndicator) { if (id != generalHydrochemicalIndicator.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(generalHydrochemicalIndicator); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GeneralHydrochemicalIndicatorExists(generalHydrochemicalIndicator.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(generalHydrochemicalIndicator)); }
public async Task <IActionResult> Create([Bind("Id,LakeId,Year,LakePart,Mineralization,TotalHardness,DissOxygWater,PercentOxygWater,pH,OrganicSubstances")] GeneralHydrochemicalIndicator generalHydrochemicalIndicator) { if (ModelState.IsValid) { _context.Add(generalHydrochemicalIndicator); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(generalHydrochemicalIndicator)); }
public async Task <IActionResult> Upload(bool FirstRowHeader, IFormFile File) { try { string sContentRootPath = _hostingEnvironment.WebRootPath; sContentRootPath = Path.Combine(sContentRootPath, "Uploads"); DirectoryInfo di = new DirectoryInfo(sContentRootPath); foreach (FileInfo filed in di.GetFiles()) { try { filed.Delete(); } catch { } } string path_filename = Path.Combine(sContentRootPath, Path.GetFileName(File.FileName)); using (var stream = new FileStream(Path.GetFullPath(path_filename), FileMode.Create)) { await File.CopyToAsync(stream); } FileInfo fileinfo = new FileInfo(Path.Combine(sContentRootPath, Path.GetFileName(path_filename))); using (ExcelPackage package = new ExcelPackage(fileinfo)) { int start_row = 1; if (FirstRowHeader) { start_row++; } List <GeneralHydrochemicalIndicator> generalHydrochemicalIndicators = new List <GeneralHydrochemicalIndicator>(); for (int i = start_row; ; i++) { if (package.Workbook.Worksheets.FirstOrDefault().Cells[i, 1].Value == null) { break; } GeneralHydrochemicalIndicator generalHydrochemicalIndicator = new GeneralHydrochemicalIndicator(); try { generalHydrochemicalIndicator.LakeId = Convert.ToInt32(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 1].Value); generalHydrochemicalIndicator.Year = Convert.ToInt32(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 2].Value); generalHydrochemicalIndicator.LakePart = (LakePart)Convert.ToInt32(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 3].Value); generalHydrochemicalIndicator.Mineralization = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 4].Value == null ? (decimal?)null : Convert.ToDecimal(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 4].Value); generalHydrochemicalIndicator.TotalHardness = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 5].Value == null ? (decimal?)null : Convert.ToDecimal(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 5].Value); generalHydrochemicalIndicator.DissOxygWater = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 6].Value == null ? (decimal?)null : Convert.ToDecimal(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 6].Value); generalHydrochemicalIndicator.PercentOxygWater = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 7].Value == null ? (decimal?)null : Convert.ToDecimal(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 7].Value); generalHydrochemicalIndicator.pH = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 8].Value == null ? (decimal?)null : Convert.ToDecimal(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 8].Value); generalHydrochemicalIndicator.OrganicSubstances = package.Workbook.Worksheets.FirstOrDefault().Cells[i, 9].Value == null ? (decimal?)null : Convert.ToDecimal(package.Workbook.Worksheets.FirstOrDefault().Cells[i, 9].Value); } catch (Exception e) { ViewBag.Error = $"{_sharedLocalizer["Row"]} {i.ToString()}: " + e.Message + (e.InnerException == null ? "" : ": " + e.InnerException.Message); break; } generalHydrochemicalIndicators.Add(generalHydrochemicalIndicator); _context.Add(generalHydrochemicalIndicators.LastOrDefault()); } if (string.IsNullOrEmpty(ViewBag.Error)) { _context.SaveChanges(); ViewBag.Report = $"{_sharedLocalizer["UploadedCount"]}: {generalHydrochemicalIndicators.Count()}"; } } foreach (FileInfo filed in di.GetFiles()) { try { filed.Delete(); } catch { } } } catch (Exception e) { if (File != null) { ViewBag.Error = e.Message + (e.InnerException == null ? "" : ": " + e.InnerException.Message); } } return(View()); }