Пример #1
0
 public IActionResult Create(Book book)
 {
     if (ModelState.IsValid)
     {
         try
         {
             if (context.Books.Contains(book))
             {
                 context.Entry(book).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
             }
             else
             {
                 context.Books.Add(book);
             }
             context.SaveChanges();
         }
         catch
         {
             return(View());
         }
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View());
     }
 }
Пример #2
0
        public ActionResult Create([Bind(Include = "ID,Title,Author,Pages,Genre,Price")] Novel novel)
        {
            if (ModelState.IsValid)
            {
                db.Novels.Add(novel);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(novel));
        }
Пример #3
0
        public ActionResult Create([Bind(Include = "userID,ISBN,Name,Author,Published")] Books books)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(books);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(books));
        }
Пример #4
0
        public ActionResult Create([Bind(Include = "userID,ISBN,imgPath")] Bookimage bookimage)
        {
            if (ModelState.IsValid)
            {
                db.Bookimage.Add(bookimage);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(bookimage));
        }
Пример #5
0
        public void Refresh()
        {
            var query = from r in db.Reservations
                        where (r.EndOfReservation < DateTime.Now)
                        select r;
            List <int> listID = new List <int>();

            foreach (var item in query.ToList())
            {
                listID.Add(item.BookID);
                db.Reservations.Remove(item);
            }
            db.SaveChanges();

            var bookQry = from b in db.Books
                          select b;

            foreach (var itm in bookQry.ToList())
            {
                if (listID.Contains(itm.ID))
                {
                    itm.Quantity++;
                    db.Entry(itm).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }
Пример #6
0
        public ActionResult Index(int bookId, string category, string searchString, int resultsPerPage = 10, int page = 1)
        {
            var      book = db.Books.Single(b => b.ID == bookId);
            BookCard bc;
            bool     conflict = false;

            //no one is reading it? borrow!
            if (book.Reader == null)
            {
                bc = new BookCard
                {
                    BookID     = bookId,
                    BorrowDate = DateTime.Now,
                    Reader     = User.Identity.Name
                };
                book.Reader = User.Identity.Name;
                db.Cards.Add(bc);
                db.Entry(book).State = EntityState.Modified;
            }
            //user is the reader? return!
            else if (book.Reader == User.Identity.Name)
            {
                bc = db.Cards.Single(c => c.BookID == bookId && c.ReturnDate == null);

                bc.ReturnDate        = DateTime.Now;
                book.Reader          = null;
                db.Entry(bc).State   = EntityState.Modified;
                db.Entry(book).State = EntityState.Modified;
            }
            else //this will probably only happen if the book was borrowed by someone else
                 //after the page was loaded but before the button click
            {
                conflict = true;
            }
            db.SaveChanges();

            if (Request.IsAjaxRequest())
            {
                return(Content((HtmlHelpers.GetButton(bookId, book.Reader, User.Identity.Name, conflict)).ToHtmlString()));
            }

            var books = db.Books.Where(b => b.Status == true).ToList();

            var model = BuildIndexViewModel(category, searchString, resultsPerPage, page, books);

            return(View(model));
        }
Пример #7
0
        public IActionResult Create(Book book)
        {
            int id = book.AutherID == 0 ? 1 : book.AutherID;

            book.AutherID = id;
            if (book != null)
            {
                //book.AutherID = id; // 不需要添加
                myDBContext.Add(book);
                //myDBContext.Books.Add(book);// 建议一般这样写 比较具体不用推断类型
                myDBContext.SaveChanges();
                //return RedirectToAction(nameof(Index));
            }



            return(Redirect("Index"));
        }
        public IActionResult Index()
        {
            //var books = _BooksDBContext.Books.ToList();
            var book = new Books()
            {
                Name = "zhangsan1",

                Price  = 24,
                Author = "sanmao",
            };

            _BooksDBContext.Books.Add(book);
            _BooksDBContext.SaveChanges();
            //User user = new User() { Name = "Herry", CreateTime = DateTime.Now };
            //context.User.Add(user);
            return(View());
        }
Пример #9
0
 public void AddBook(Book book)
 {
     _db.Add(book);
     _db.SaveChanges();
 }