public ActionResult Create(Subscribe subscribe) { var rep = new DataRepositories(); if (!rep.Books.GetAll().Any(b => b.BookID == subscribe.BookID)) { ModelState.AddModelError(string.Empty, "Book ID is invalid"); } else { if (!rep.Books.GetSingle(subscribe.BookID).InStorage) ModelState.AddModelError(string.Empty, "This book isn't in storage"); } if (!rep.Subscribers.GetAll().Any(s => s.SubscriberID == subscribe.SubscriberID)) ModelState.AddModelError(string.Empty, "Subscriber ID is invalid"); if (ModelState.IsValid) { subscribe.LibrarianID = rep.Users.GetSingle(WebSecurity.CurrentUserId).Librarian.LibrarianID; subscribe.DeliveryDate = DateTime.Now; subscribe.Status = false; rep.Subscribes.Add(subscribe); var b = rep.Books.GetSingle(subscribe.BookID); if (b.Subscribes.Where(s=>!s.Status).Count() >= b.Quantity) b.InStorage = false; else b.InStorage = true; rep.Books.Update(b); return RedirectToAction("Index"); } return View(subscribe); }
// // GET: /Subscribe/Create public ActionResult Create(int Id = 0) { var s = new Subscribe(); s.BookID = Id; return View(s); }
public ActionResult Edit(Subscribe subscribe) { if (ModelState.IsValid) { db.Entry(subscribe).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.BookID = new SelectList(db.Books, "BookID", "Title", subscribe.BookID); ViewBag.SubscriberID = new SelectList(db.Subscribers, "SubscriberID", "FirstName", subscribe.SubscriberID); ViewBag.LibrarianID = new SelectList(db.Librarians, "LibrarianID", "FirstName", subscribe.LibrarianID); return View(subscribe); }