Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 public void Update(Book book)
 {
     context.Entry(book).State = System.Data.EntityState.Modified;
 }
Exemplo n.º 3
0
 public void Insert(Book book)
 {
     context.Books.Add(book);
 }
Exemplo n.º 4
0
 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);
 }