public ActionResult AddOneBook(Book newBook) { //if (ModelState.IsValid) //{ // db.Books.Add(newBook); // db.SaveChanges(); // return RedirectToAction("BookList"); //} //else // return View(newBook); try { if (ModelState.IsValid) { unitOfWork.BookRepository.Insert(newBook); unitOfWork.Save(); return RedirectToAction("BookList"); } } catch (DataException) { //Log the error (add a variable name after DataException) ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return View(newBook); }
public void Update(Book book) { context.Entry(book).State = System.Data.EntityState.Modified; }
public void Insert(Book book) { context.Books.Add(book); }
public ActionResult Edit(Book model) { try { unitOfWork.BookRepository.Update(model); unitOfWork.Save(); return RedirectToAction("Detail", new { bookid = model.BookId}); } catch (Exception ex) { ModelState.AddModelError("", "修改失败,请查看详细错误信息。"); } return View(model); }