コード例 #1
0
        //takes an Album as a parameter and adds it to the user’s cart. Since the Cart table tracks quantity for each album
        //, it includes logic to create a new row if needed or just increment the quantity if the user has already ordered one copy of the album.
        public void AddToCart(Album album)
        {
            //get the matching cart and album instance
            var cartItem = storeDB.Carts.SingleOrDefault(
                    c => c.CartID == ShoppingCartID && c.AlbumID == album.AlbumID);

            if (cartItem == null)
            {
                //create a new cart item
                cartItem = new Cart
                {
                    AlbumID = album.AlbumID,
                    CartID = ShoppingCartID,
                    Count = 1,
                    DateCreated = DateTime.Now
                };
                storeDB.Carts.Add(cartItem);

            }
            else
            { //just increase the count
                cartItem.Count++;
            }

            //save the change to db
            storeDB.SaveChanges();
        }
コード例 #2
0
        public ActionResult Create(Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.GenreID = new SelectList(db.Genres, "GenreId", "Name", album.GenreID);
            ViewBag.ArtistID = new SelectList(db.Artists, "ArtistID", "Name", album.ArtistID);
            return View(album);
        }
コード例 #3
0
 public ActionResult Edit(Album album)
 {
     if (ModelState.IsValid)
     {
         db.Entry(album).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.GenreID = new SelectList(db.Genres, "GenreId", "Name", album.GenreID);
     ViewBag.ArtistID = new SelectList(db.Artists, "ArtistID", "Name", album.ArtistID);
     return View(album);
 }