// 获取购物车列表 public static List <Models.CartModel> GetCarts(string username) { SqlParameter[] sqlParameters = new SqlParameter[] { new SqlParameter("@Username", username) }; string sql = "select item.item_id,item_name,item_picture,item_price,item_sales,item_number,cart_item_number,cart_intime from item inner join cart on item.item_id = cart.item_id where cart.username = @Username"; SqlDataReader sqlData = SqlHelper.ExecuteTable(sql, sqlParameters); if (sqlData == null) { return(null); } List <Models.CartModel> result = new List <Models.CartModel>(); while (sqlData.Read()) { Models.CartModel model = new Models.CartModel(); model.ItemId = sqlData.GetInt32(0); model.ItemName = sqlData.GetString(1); model.ItemPicture = sqlData.GetString(2); model.ItemPrice = sqlData.GetDouble(3); model.ItemSales = sqlData.GetInt32(4); model.ItemNumber = sqlData.GetInt32(5); model.CartItemNumber = sqlData.GetInt32(6); model.CartInTime = sqlData.GetDateTime(7); result.Add(model); } sqlData.Close(); return(result); }
public ActionResult Cart(Models.CartModel p) { using (var connection = new MySqlConnection(this.connectionString)) { // See if product already is in cart var exists = connection.QuerySingleOrDefault <CartModel>("SELECT name FROM cart WHERE name = @product LIMIT 1", new { product = p.Name }); if (exists != null) { // Update Amount connection.Query <CartModel>("UPDATE cart SET amount = amount +1 WHERE name = @product", new { product = p.Name }); } else { // Add Product into Cart connection.Query <CartModel>("INSERT INTO cart(`UserID`, `Name`, `Price`) VALUES(@user, @item, @p)", new { user = OnlineList.User, item = p.Name, p = p.Price }); } // Reset List with updated content this.ShopList = connection.Query <CartModel>("SELECT * FROM cart").ToList(); // Increase Users Amount of products counter OnlineList.UpdateAmount(); } return(View(ShopList)); }
public async Task <ActionResult> Cart() { var userId = User.Identity.GetUserId(); var _productList = await dataLayer.getProductsListFromCart(userId, null); decimal _total = 0; foreach (var o in _productList) { if (o.IsSelected) { o.IsSelected = await dataLayer.setCartItemSelection(o.CartId, o.IsSelected, userId); if (!o.IsSelected) { o.Status = "out of stock"; } } if (o.Quantity == 1) { _total = _total + o.Amount; } else { _total = _total + (o.Amount * o.Quantity); } } CartModel _model = new Models.CartModel() { ProductList = _productList }; return(View(_model)); }