public void AddOrUpdateBook(Book book)
 {
     if (book.Id == default(int))
         context.Books.Add(book);
     else
         context.Entry(book).State = EntityState.Modified;
 }
 public async Task<IActionResult> Edit(Book book)
 {
     if (ModelState.IsValid)
     {
         _context.Update(book);
         await _context.SaveChangesAsync();
         return RedirectToAction("Index");
     }
     ViewData["AuthorID"] = new SelectList(_context.Set<Author>(), "AuthorID", "Author", book.AuthorID);
     return View(book);
 }
Пример #3
0
 public IActionResult Create(Book book)
 {
     if (ModelState.IsValid)
     {
         _context.Book.Add(book);
         _context.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewData["AuthorID"] = new SelectList(_context.Set<Author>(), "AuthorID", "Author", book.AuthorID);
     return View(book);
 }
Пример #4
0
 public IActionResult Create(Book book)
 {
     if (ModelState.IsValid)
     {
         db.AddOrUpdateBook(book);
         db.SaveChanges();
         TempData["success"] = $"New book added successfully with Id = {book.Id}";
         return RedirectToAction("Index");
     }
     AddAuthorDropdown(book.AuthorId);
     return View(book);
 }