public async Task <IActionResult> OnPostAsync() { if (this.Image != null) { //Set Key Name string ImageName = Guid.NewGuid().ToString() + Path.GetExtension(Image.FileName); Books.BookCover = ImageName; //Get url To Save string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images/Books", ImageName); using (var stream = new FileStream(SavePath, FileMode.Create)) { Image.CopyTo(stream); } } if (!ModelState.IsValid) { return(Page()); } _context.Books.Add(Books); await _context.SaveChangesAsync(); return(RedirectToPage("./ListBooks")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Books = await _context.Books.FindAsync(id); if (Books != null) { _context.Books.Remove(Books); await _context.SaveChangesAsync(); } return(RedirectToPage("./ListBooks")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (this.Image != null) { //Set Key Name string ImageName = Guid.NewGuid().ToString() + Path.GetExtension(Image.FileName); Users.Photo = ImageName; //Get url To Save string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images/Users", ImageName); using (var stream = new FileStream(SavePath, FileMode.Create)) { Image.CopyTo(stream); } } if (!ModelState.IsValid) { return(Page()); } _context.Attach(Users).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UsersExists(Users.Email)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./ListBooks")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (this.Image != null) { //Set Key Name string ImageName = Guid.NewGuid().ToString() + Path.GetExtension(Image.FileName); Users.Photo = ImageName; //Get url To Save string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images/Users", ImageName); using (var stream = new FileStream(SavePath, FileMode.Create)) { Image.CopyTo(stream); } } if (!ModelState.IsValid) { return(Page()); } try { _context.Users.Add(Users); await _context.SaveChangesAsync(); } catch (DbUpdateException ex) when((ex.InnerException as SqlException)?.Number == 2627) { return(RedirectToPage("./SignUp")); } return(RedirectToPage("./Index")); }