public void AddToCart(int albumId) { // TODO: Verify that the Album Id exists in the database. Cart cartItem = db.Carts.SingleOrDefault(c => c.CartId == this.ShoppingCartId && c.AlbumId == albumId); if (cartItem == null) { // Item is not in cart; add new cart item cartItem = new Cart() { CartId = this.ShoppingCartId, AlbumId = albumId, Count = 1, DateCreated = DateTime.Now }; db.Carts.Add(cartItem); } else { // Item is already in cart; increase item count (quantity) cartItem.Count++; } db.SaveChanges(); }
public void AddToCart(Album album) { // Get the matching cart and album instances var cartItem = storeDB.Carts.SingleOrDefault( c => c.CartId == ShoppingCartId && c.AlbumId == album.AlbumId); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new Cart { AlbumId = album.AlbumId, CartId = ShoppingCartId, Count = 1, DateCreated = DateTime.Now }; storeDB.Carts.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Count++; } // Save changes storeDB.SaveChanges(); }
/*将专辑对象作为参数传入方法,添加专辑到购物车中,在 Cart 表中跟踪每个专辑的数量, *在这个方法中,我们将会检查是在表中增加一个新行,还是仅仅在用户已经选择的专辑上增加数量*/ public void AddToCart(Album album) { // Get the matching cart and album instances //检查该专辑是否已经在购物车中 var cartItem = storeDB.Carts.SingleOrDefault( c => c.CartId == ShoppingCartId && c.AlbumId == album.AlbumId);//该购物车id是否为我的购物车,该专辑是否在购物车中有了。(cards表里包含了所有人的购物车) if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new Cart { AlbumId = album.AlbumId, CartId = ShoppingCartId, Count = 1, DateCreated = DateTime.Now }; storeDB.Carts.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity //保存到数据库中 cartItem.Count++; } // Save changes storeDB.SaveChanges(); }
public ActionResult Create([Bind(Include = "ArtistId,Name")] Artist artist) { if (ModelState.IsValid) { db.Artists.Add(artist); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(artist)); }
public ActionResult Create([Bind(Include = "GenreId,Name,Description")] Genre genre) { if (ModelState.IsValid) { db.Genres.Add(genre); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(genre)); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) { if (ModelState.IsValid)//检查表单的数据是否通过了验证规则 { //如果表单通过了验证,保存数据,然后显示更新之后的专辑列表,EF 将会生成适当的 SQL 命令来持久化对象 db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } //如果表单没有通过验证,重新显示带有验证提示信息的表单 ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); return(View(album)); }