示例#1
0
        // GET: Books/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Book book = await _context.Books.FindAsync(id);

            if (book == null)
            {
                return(NotFound());
            }

            BooksEditViewModel model = new BooksEditViewModel
            {
                Id     = book.Id,
                ISBN   = book.ISBN,
                Author = book.Author,
                Pages  = book.Pages,
                Title  = book.Title,
                Price  = book.Price
            };

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> Edit(BooksEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                Book book = new Book
                {
                    ISBN   = model.ISBN,
                    Author = model.Author,
                    Pages  = model.Pages,
                    Title  = model.Title,
                    Price  = model.Price
                };

                try
                {
                    _context.Update(book);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BookExists(book.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }