public ActionResult Edit(int id) { var context = new ApplicationDbContext(); var book = context.Books.FirstOrDefault(b => b.BookId == id); BookModel model = new BookModel() { BookId = book.BookId - 1, Author = book.Author, CategoryId = book.Category.CategoryId, Description = book.Description, ISBN = book.ISBN, Title = book.Title, WebSite = book.WebSite }; var categories = context.Categories.ToList(); model.DropDownItems = categories.Select(category => new SelectListItem() { Value = category.CategoryId.ToString(), Text = category.Title }).ToList(); return View(model); }
public ActionResult Edit(int id, BookModel model) { var context = new ApplicationDbContext(); var book = context.Books.Find(id); book.Author = model.Author; book.Description = model.Description; book.ISBN = model.ISBN; book.Title = model.Title; book.WebSite = model.WebSite; Category category = context.Categories.FirstOrDefault(c => c.CategoryId == model.CategoryId); book.Category = category; context.SaveChanges(); return RedirectToAction("Index"); }