public ActionResult Index(Models.OrderModel.Ship model)
        {
            if (this.ModelState.IsValid)
            {
                // 取得目前購物車
                var CurrentCart = Models.Carts.Operation.GetCurrentCart();

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

                using (Models.CartsEntities db = new Models.CartsEntities())
                {
                    // 建立 Order 物件
                    var order = new Models.Order()
                    {
                        UserId          = UserId,
                        ReceiverName    = model.ReceiverName,
                        ReceiverPhone   = model.ReceiverPhone,
                        ReceiverAddress = model.ReceiverAddress
                    };

                    // 加入 Order 資料表後,儲存變更
                    db.Orders.Add(order);
                    db.SaveChanges();

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

                    // 將 OrderDetail 物件,加入 OrderDetail 資料表後,儲存變更。
                    db.OrderDetails.AddRange(orderDetails);
                    db.SaveChanges();
                }
                return(Content("訂購成功"));
            }
            return(View());
        }
Exemplo n.º 2
0
        public ActionResult Delete(int id)
        {
            using (Models.CartsEntities db = new Models.CartsEntities())
            {
                //抓取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"));
                }
            }
        }
Exemplo n.º 3
0
        public ActionResult Delete(int id)
        {
            using (Models.CartsEntities db = new Models.CartsEntities())
            {
                //抓取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("Succeed to delete product [{0}]", result.Name);
                    return(RedirectToAction("Index"));
                }
                else
                {   //如果沒有資料則顯示錯誤訊息並導回Index頁面
                    TempData["resultMessage"] = "Data does not exist,cannot remove it,please try it again";
                    return(RedirectToAction("Index"));
                }
            }
        }
Exemplo n.º 4
0
        // GET: Product
        public ActionResult Index(string q, int page = 1, int pagesize = 6)
        {
            //List實體化
            //List<Models.ViewModel.ProductCategoryViewModel> result = new List<Models.ViewModel.ProductCategoryViewModel>();

            //接收轉至的成功訊息
            ViewBag.ResultMessage = TempData["ResultMessage"];
            //搜尋欄是否為空或空白
            if (string.IsNullOrWhiteSpace(q))
            {
                using (Models.CartsEntities db = new Models.CartsEntities())
                {
                    //db.Product和db.Category做inner join,select new Models.ViewModel.ProductCategoryViewModel
                    var result = (from s in db.ProductSet
                                  join o in db.CategorySet on s.CategoryId equals o.Id
                                  orderby s.Id
                                  select new  ProductCategoryViewModel
                    {
                        Id = s.Id,
                        Name = s.Name,
                        CategoryId = o.Id,
                        CategoryName = o.Name,
                        DefaultImageId = s.DefaultImageId,
                        DefaultImageURL = s.DefaultImageURL,
                        Status = s.Status,
                        Description = s.Description,
                        Price = s.Price,
                        PublishDate = s.PublishDate,
                        Quantity = s.Quantity
                    });

                    return(View(result.ToPagedList(page, pagesize)));
                }
            }
            else
            {
                using (Models.CartsEntities db = new Models.CartsEntities())
                {
                    //db.Product和db.Category做inner join,select new Models.ViewModel.ProductCategoryViewModel
                    var result = (from s in db.ProductSet
                                  join o in db.CategorySet on s.CategoryId equals o.Id
                                  orderby s.Id
                                  where s.Name.Contains(q)
                                  select new Models.ViewModel.ProductCategoryViewModel
                    {
                        Id = s.Id,
                        Name = s.Name,
                        CategoryId = o.Id,
                        CategoryName = o.Name,
                        DefaultImageId = s.DefaultImageId,
                        DefaultImageURL = s.DefaultImageURL,
                        Status = s.Status,
                        Description = s.Description,
                        Price = s.Price,
                        PublishDate = s.PublishDate,
                        Quantity = s.Quantity
                    });

                    return(View(result.ToPagedList(page, pagesize)));
                }
            }
        }