Esempio n. 1
0
        public ActionResult Edit(Models.Product postback)
        {
            if (this.ModelState.IsValid) //判斷使用者輸入資料是否正確
            {
                using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
                {
                    //抓取Product,ID等於回傳postback.Id的資料
                    var result = (from s in db.Products where s.Id == postback.Id select s).FirstOrDefault();

                    //儲存使用者變更資料
                    result.Name            = postback.Name; result.Price = postback.Price;
                    result.PublishDate     = postback.PublishDate; result.Quantity = postback.Quantity;
                    result.Status          = postback.Status; result.CategoryID = postback.CategoryID;
                    result.DefaultImageId  = postback.DefaultImageId; result.Description = postback.Description;
                    result.DefaultImageURL = postback.DefaultImageURL;

                    //儲存所有變更
                    db.SaveChanges();

                    //設定成功訊息並導回Index頁面
                    TempData["ResultMessage"] = string.Format("商品{0}成功編輯", postback.Name);
                    return(RedirectToAction("Index"));
                }
            }
            else //如果資料不正確則導向自己(Edit頁面)
            {
                return(View(postback));
            }
        }
Esempio n. 2
0
        [HttpPost] //指定只有POST方法才可進入
        public ActionResult Create(Models.Product postback)
        {
            using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
            {
                //將回傳資料postback加入至Products
                db.Products.Add(postback);

                //儲存異動資料
                db.SaveChanges();
            }
            return(View());
        }
        [Authorize] //登入會員才可留言
        public ActionResult AddComment(int id, string Content)
        {
            //取得目前登入使用者Id
            var userId = HttpContext.User.Identity.GetUserName();

            var currentDateTime = DateTime.Now;

            var comment = new Models.ProductComment()
            {
                ProductId  = id,
                Content    = Content,
                UserId     = userId,
                CreateDate = currentDateTime
            };

            using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
            {
                db.ProductComments.Add(comment);
                db.SaveChanges();
            }
            return(RedirectToAction("Details", new { id = id }));
        }
Esempio n. 4
0
        //刪除商品
        public ActionResult Delete(int id)
        {
            using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
            {
                //抓取Product.Id等於輸入id的資料
                var result = (from s in db.Products where s.Id == id select s).FirstOrDefault();
                if (result != default(Models.Product)) //判斷此id是否有資料
                {
                    db.Products.Remove(result);
                    //儲存所有變更
                    db.SaveChanges();

                    //設定成功訊息並導回index頁面
                    TempData["ResultMessage"] = String.Format("商品:{0}成功刪除", result.Name);
                    return(RedirectToAction("Index"));
                }
                else
                { //如果沒有資料則顯示錯誤訊息並導回Index頁面
                    TempData["ResultMessage"] = "指定資料不存在,無法刪除,請重新操作";
                    return(RedirectToAction("Index"));
                }
            }
        }
Esempio n. 5
0
        public ActionResult Index(Models.OrderModel.Ship postback)
        {
            if (this.ModelState.IsValid)
            {
                //暫存訂單編號
                int Id = 0;
                //取得目前購物車
                var currentcart = Models.Cart.Operation.GetCurrentCart();

                //取得目前登入使用者Id
                var userId = HttpContext.User.Identity.GetUserId();

                using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
                {
                    //建立Order物件
                    var order = new Models.Order()
                    {
                        UserId          = userId,
                        ReceiverName    = postback.ReceiverName,
                        ReceiverPhone   = postback.ReceiverPhone,
                        ReceiverAddress = postback.ReceiverAddress
                    };
                    //加其入Orders資料表後,儲存變更
                    db.Orders.Add(order);
                    try
                    {
                        db.SaveChanges();
                        //取得訂單編號
                        Id = order.Id;
                    }
                    catch (DbEntityValidationException deEx)
                    {
                        foreach (var validationError in deEx.EntityValidationErrors)
                        {
                            foreach (var dbValidationError in validationError.ValidationErrors)
                            {
                                throw new Exception(dbValidationError.ErrorMessage);
                            }
                        }
                    }
                    //db.SaveChanges();

                    //取得購物車OrderDetail物件
                    var orderDetails = currentcart.ToOrderDetailList(order.Id);

                    //將其加入OrderDetails資料表後,儲存變更
                    db.OrderDetails.AddRange(orderDetails);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (DbEntityValidationException deEx)
                    {
                        foreach (var validationError in deEx.EntityValidationErrors)
                        {
                            foreach (var dbValidationError in validationError.ValidationErrors)
                            {
                                throw new Exception(dbValidationError.ErrorMessage);
                            }
                        }
                    }
                    //取得購物車選擇商品數量
                    var productAmount = currentcart.Select(c => new { c.Id, c.Quantity }).ToList();
                    //將商品庫存檢調購物車數量後,更新至資料庫
                    foreach (var item in productAmount)
                    {
                        var result = (from s in db.Products where s.Id == item.Id select s).FirstOrDefault();
                        result.Quantity = result.Quantity - item.Quantity;
                        db.SaveChanges();
                    }
                }


                currentcart.ClearCart();
                TempData["Success"] = "訂購成功";
                return(RedirectToAction("MyOrderDetail", new { id = Id }));
            }
            return(View());
        }