コード例 #1
0
        public ActionResult Save(Book book)
        {
            bool status = false;

            if (ModelState.IsValid)
            {
                if (book.BookID > 0)
                {
                    //Edit
                    var v = db.Books.Where(a => a.BookID == book.BookID).FirstOrDefault();
                    if (v != null)
                    {
                        v.BookID     = book.BookID;
                        v.Title      = book.Title;
                        v.CategoryID = book.CategoryID;
                        v.ISBN       = book.ISBN;
                        v.Author     = book.Author;
                        v.Stock      = book.Stock;
                        v.Price      = book.Price;
                    }
                }
                else
                {
                    //Save
                    db.Books.Add(book);
                }
                db.SaveChanges();
                status = true;
            }
            return(new JsonResult {
                Data = new { status }
            });
        }
コード例 #2
0
        public ActionResult Index(Message message)
        {
            if (ModelState.IsValid)
            {
                message.MessageDateTime = DateTime.Now;

                context.Messages.Add(message);
                context.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View(message));
        }
コード例 #3
0
ファイル: Purchase.aspx.cs プロジェクト: JoelFong85/ASPNET_CA
 protected void Button1_Click(object sender, EventArgs e)
 {
     ListItem[] titles = (ListItem[])Session["buyTitles"];
     ListItem[] quan   = (ListItem[])Session["buyQty"];
     for (int i = 0; i < titles.Length; i++)
     {
         string  title     = titles[i].Text;
         string  qty       = quan[i].Text;
         Mybooks b         = new Mybooks();
         Book    book      = b.Books.Where(x => x.Title == title).First();
         int     buyAmount = Convert.ToInt32(qty);
         book.Stock = book.Stock - buyAmount;
         b.SaveChanges();
     }
     Response.Redirect("BooksUser.aspx");
 }
コード例 #4
0
        public static void CheckoutCart()
        {
            List <CartBook> checkoutList = (List <CartBook>)HttpContext.Current.Session[HttpContext.Current.User.Identity.GetUserId()];

            foreach (var book in checkoutList)
            {
                var bookToCheckout = context.Books.Where(a => a.ISBN == book.Isbn).FirstOrDefault();
                if (bookToCheckout != null)
                {
                    bookToCheckout.Stock = bookToCheckout.Stock - book.Quantity;
                }

                context.SaveChanges();
            }

            HttpContext.Current.Session[HttpContext.Current.User.Identity.GetUserId()]           = new List <CartBook>();
            HttpContext.Current.Session["total" + HttpContext.Current.User.Identity.GetUserId()] = 0;
        }