public ActionResult Create([Bind(Include = "borrowId,status,burrowTime,ygBackTime,sjBackTime,borrowOper,backOper,bookId,readerId")] BorrowItem borrowitem) { borrowitem.sjBackTime = new DateTime(2000, 01, 01); borrowitem.burrowTime = DateTime.Now; borrowitem.ygBackTime = DateTime.Now.AddMonths(+3); if (Session["userName"] != null) { borrowitem.borrowOper = Session["userName"].ToString(); } else { return(RedirectToAction("Login", "User")); } borrowitem.status = Status.在借; ViewBag.bookId = new SelectList(db.books, "bookId", "bookName"); ViewBag.readerId = new SelectList(db.readers, "readerId", "readerName"); try { if (ModelState.IsValid) { int _bookID = borrowitem.bookId; int _readerID = borrowitem.readerId; IBorrowItemDal BorrowItemDal = RepositoryFactory.BorrowRepository; if (!BorrowItemDal.judgeBook(_bookID)) { ModelState.AddModelError("bookId", "book amount is not enough"); return(View(borrowitem)); } if (!BorrowItemDal.judgeReader(_readerID)) { ModelState.AddModelError("readerId", "balance or enable borrow amount is not enough"); return(View(borrowitem)); } BorrowItemDal.borrowBookSuccess(_readerID, _bookID); db.borrowItems.Add(borrowitem); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.bookId = new SelectList(db.books, "bookId", "bookName", borrowitem.bookId); ViewBag.readerId = new SelectList(db.readers, "readerId", "readerName", borrowitem.readerId); } catch (Exception e) { LogHelper.WriteLog(typeof(BorrowItemController), e); throw e; } return(View(borrowitem)); }
public ActionResult Create(Book book) { try { List <SelectListItem> items = Common.getBookTypeItems(); this.ViewData["list"] = items; string Type = Request.Form["list"]; if (Type == null) { ModelState.AddModelError("type", "not null"); } else { book.type = Type; } if (Session["userName"] != null) { book.addName = Session["userName"].ToString(); } else { return(RedirectToAction("Login", "User")); } book.addTime = DateTime.Now; if (ModelState.IsValid) { book.currAmount = book.amount; db.books.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (Exception e) { LogHelper.WriteLog(typeof(BookController), e); throw e; } return(View(book)); //return Json(new { success = false, errors = ModelState }); //return PartialView("EditorTemplates/Dialog", book); }
public ActionResult Create(Reader reader) { reader.createTime = DateTime.Now; if (ModelState.IsValid) { try { db.readers.Add(reader); db.SaveChanges(); } catch (Exception e) { LogHelper.WriteLog(typeof(ReaderController), e); throw e; } return(RedirectToAction("Index")); } return(View(reader)); }
public ActionResult Register(RegisterAndLoginViewModel userViewModel) { try { User user = new User() { UserName = userViewModel.RegisterViewModel.UserName, Password = userViewModel.RegisterViewModel.Password, DisplayName = userViewModel.RegisterViewModel.DisplayName, EmailAddress = userViewModel.RegisterViewModel.EmailAddress }; if (ModelState.IsValid) { if (userService.Exist(user.UserName)) { ModelState.AddModelError("UserName", "用户名已存在!"); return(View("LoginAndRegister", userViewModel)); } else if (user != null) { db.users.Add(user); db.SaveChanges(); return(RedirectToAction("Login")); } else { ModelState.AddModelError("", "error"); return(View("LoginAndRegister", userViewModel)); } } } catch (Exception e) { LogHelper.WriteLog(typeof(UserController), e); throw e; } return(View(userViewModel)); }
public T Add(T entity) { nContext.Entry <T>(entity).State = EntityState.Added; nContext.SaveChanges(); return(entity); }