// GET: Product public ActionResult Index() { //宣告回傳商品列表result List <Models.Database.Product> result = new List <Models.Database.Product>(); //接收轉導的成功訊息 ViewBag.ResultMessage = TempData["ResultMessage"]; //使用CartsEntities類別,名稱為db using (Models.Database._1MVC1Model db = new _1MVC1Model()) { //使用LinQ語法抓取目前Products資料庫中所有資料 result = (from s in db.Products select s).ToList(); //將result傳送給檢視 return(View(result)); } }
public bool AddProduct(int ProductId) { var findItem = this.cartItems .Where(s => s.PSID == ProductId) .Select(s => s) .FirstOrDefault(); //判斷相同Id的CartItem是否已經存在購物車內 if (findItem == default(Models.Carts.CartItem)) { //不存在購物車內,則新增一筆 using (Models.Database._1MVC1Model db = new _1MVC1Model()) { var product = ( from ps in db.ProductSpecifications where ps.ProductSpecificationID == ProductId select new ChartViewModels { ProductSpecificationID = ps.ProductSpecificationID, ProductID = ps.ProductID, Price = ps.Price, Colour = ps.Colour, Image = ps.Image, ProductName = ps.Product.ProductName, Quantity = ps.Product.Quantity, }).FirstOrDefault(); if (product != default(ViewModels.ChartViewModels)) { this.AddProduct(product); } } } else { //存在購物車內,則將商品數量累加 findItem.Quantity += 1; } return(true); }