public async Task <IActionResult> OnPostDelete(int id) { var book = await BookListRazorContext.Book.FindAsync(id); if (book == null) { return(NotFound()); } try { var reviews = BookListRazorContext.Review.Where(x => x.BookId == id).ToList(); foreach (Review review in reviews) { BookListRazorContext.Review.Remove(review); } await BookListRazorContext.SaveChangesAsync(); } catch { } try { var items = BookListRazorContext.Cart.Where(x => x.BookId == id).ToList(); foreach (Cart item in items) { BookListRazorContext.Cart.Remove(item); } await BookListRazorContext.SaveChangesAsync(); } catch { } BookListRazorContext.Book.Remove(book); await BookListRazorContext.SaveChangesAsync(); return(RedirectToPage("Index")); }
public async Task OnGetAsync() { // Books = await BookListRazorContext.Book.ToListAsync(); string url = Request.GetDisplayUrl(); if (url.Contains("?id=-1")) { BookListRazorContext.Database.ExecuteSqlCommand("TRUNCATE TABLE [Cart]"); await BookListRazorContext.SaveChangesAsync(); } else { try { int id = Convert.ToInt32(url.Substring(url.IndexOf("?id=") + 4, url.IndexOf("&handler") - url.IndexOf("?id=") - 4)); var book = await BookListRazorContext.Book.FindAsync(id); Cart cart = new Cart(); cart.BookId = book.Id; cart.Name = book.Name; cart.Price = book.Price; BookListRazorContext.Cart.Add(cart); await BookListRazorContext.SaveChangesAsync(); } catch { } } Items = await BookListRazorContext.Cart.ToListAsync(); }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { // Review.BookId = Convert.ToInt32(Context.Request.Query["id"]); string url = Request.GetDisplayUrl(); //string id = a.Substring(a.IndexOf("?id=") + 4, a.IndexOf("&handler") - a.IndexOf("?id=") - 4); int afterIndex = url.IndexOf("&handler") - url.IndexOf("?id=") - 4; if (afterIndex > 0) { Review.BookId = Convert.ToInt32(url.Substring(url.IndexOf("?id=") + 4, afterIndex)); } else { Review.BookId = Convert.ToInt32(url.Substring(url.IndexOf("?id=") + 4)); } await db.Review.AddAsync(Review); await db.SaveChangesAsync(); return(RedirectToPage("Shop")); } else { return(Page()); } }
public async Task <IActionResult> OnPostDelete(int id) { var book = await _db.Book.FindAsync(id); if (book == null) { return(NotFound()); } _db.Book.Remove(book); await _db.SaveChangesAsync(); return(RedirectToPage("Index")); }
public async Task <IActionResult> Delete(int id) { var bookFromDb = await _db.Book.FirstOrDefaultAsync(u => u.Id == id); if (bookFromDb == null) { return(Json(new { success = false, message = "Error while Deleting" })); } _db.Book.Remove(bookFromDb); await _db.SaveChangesAsync(); return(Json(new { success = true, message = "Delete successful" })); }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { await db.Book.AddAsync(Book); await db.SaveChangesAsync(); return(RedirectToPage("Index")); } else { return(Page()); } }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { var BookFromDb = await _db.Book.FindAsync(Book.Id); BookFromDb.Name = Book.Name; BookFromDb.ISBN = Book.ISBN; BookFromDb.Author = Book.Author; await _db.SaveChangesAsync(); return(RedirectToPage("Index")); } return(RedirectToPage()); }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { var BookEdit = await db.Book.FindAsync(Book.Id); BookEdit.Name = Book.Name; BookEdit.Author = Book.Author; BookEdit.Isbn = Book.Isbn; await db.SaveChangesAsync(); return(RedirectToPage("Index")); } return(RedirectToPage()); }
public async Task <IActionResult> OnPost() { var book = await BookListRazorContext.Book.FindAsync(1); if (book == null) { return(NotFound()); } Cart cart = new Cart(); cart.BookId = book.Id; cart.Name = book.Name; cart.Price = book.Price; BookListRazorContext.Cart.Add(cart); await BookListRazorContext.SaveChangesAsync(); return(RedirectToPage("Cart")); }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { if (Book.Id == 0) { _db.Book.Add(Book); } else { _db.Book.Update(Book); } await _db.SaveChangesAsync(); return(RedirectToPage("Index")); } return(RedirectToPage()); }