public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Expense).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ExpenseExists(Expense.Id)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } Expense.OwnerID = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; _context.Expense.Add(Expense); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostImport() { IFormFile file = Request.Form.Files[0]; string folderName = "Upload"; string webRootPath = _hostingEnvironment.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); StringBuilder sb = new StringBuilder(); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } if (file.Length > 0) { string sFileExtension = Path.GetExtension(file.FileName).ToLower(); ISheet sheet; string fullPath = Path.Combine(newPath, file.FileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); stream.Position = 0; if (sFileExtension == ".xls") { HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } else { XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File { var expense = new Expense(); IRow row = sheet.GetRow(i); if (row == null) { continue; } if (row.Cells.All(d => d.CellType == CellType.Blank)) { continue; } if (row.GetCell(row.FirstCellNum) != null) { expense.DateOfExpense = Convert.ToDateTime(row.GetCell(row.FirstCellNum).ToString()); } if (row.GetCell(row.FirstCellNum + 1) != null) { expense.Amount = Convert.ToDecimal(row.GetCell(row.FirstCellNum + 1).ToString()); } if (row.GetCell(row.FirstCellNum + 2) != null) { expense.Category = row.GetCell(row.FirstCellNum + 2).ToString(); } if (row.GetCell(row.FirstCellNum + 3) != null) { expense.Description = row.GetCell(row.FirstCellNum + 3).ToString(); } //expense.Id = _context.Expense.Select(x => x.Id).Max() + 1; var currentUser = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; expense.OwnerID = currentUser; _context.Expense.Add(expense); } await _context.SaveChangesAsync(); } } return(RedirectToPage("./Index")); }