Пример #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));
            }
        }
 public ActionResult Index()
 {
     using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
     {
         var result = (from s in db.Products select s).ToList();
         return(View(result));
     }
 }
Пример #3
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());
        }
Пример #4
0
        public ActionResult MyOrder()
        {
            //取得目前使用者Id
            var userId = HttpContext.User.Identity.GetUserId();

            using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
            {
                var result = (from s in db.Orders
                              where s.UserId == userId
                              select s).ToList();
                return(View(result));
            }
        }
 public ActionResult Details(int id)
 {
     using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
     {
         var result = (from s in db.Products
                       where s.Id == id
                       select s).FirstOrDefault();
         if (result == default(Models.Product))
         {
             return(RedirectToAction("Index"));
         }
         else
         {
             return(View(result));
         }
     }
 }
Пример #6
0
 public ActionResult MyOrderDetail(int id)
 {
     using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
     {
         var result = (from s in db.OrderDetails
                       where s.OrderId == id
                       select s).ToList();
         if (result.Count == 0)
         {
             return(RedirectToAction("Index"));
         }
         else
         {
             return(View(result));
         }
     }
 }
Пример #7
0
        // GET: Product
        public ActionResult Index()
        {
            //宣告回傳商品列表陣列result
            List <Models.Product> result = new List <Models.Product>();

            ViewBag.ResultMessage = TempData["ResultMessage"];

            //使用CartsEntities類別,名稱為db
            using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
            {
                //使用LinQ語法抓取目前products資料庫中的資料
                result = (from s in db.Products select s).ToList();

                //將result傳送給檢視
                return(View(result));
            }
        }
Пример #8
0
 //編輯商品頁面
 public ActionResult Edit(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是否有資料
         {
             return(View(result));              //如果有id回傳編輯商品頁面
         }
         else
         {
             //如果沒有資料則顯示錯誤訊息並導回Index頁面
             TempData["ResultMessage"] = "資料有誤,請重新操作";
             return(RedirectToAction("Index"));
         }
     }
 }
Пример #9
0
        public ActionResult Details(int id)
        {
            using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
            {
                //取得OrderId為傳入id的所有商品列表
                var result = (from s in db.OrderDetails
                              where s.OrderId == id select s).ToList();

                if (result.Count == 0)
                {
                    //如果商品數目為零,代表該訂單異常(無商品),則導回商品列表
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(View(result));
                }
            }
        }
        [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 }));
        }
Пример #11
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"));
                }
            }
        }
Пример #12
0
        //public ActionResult SearchByUserName(string UserName)
        //{
        //    //儲存查詢出來的UserId
        //    string searchUserId = null;
        //    using (Models.UserEntities db = new Models.UserEntities())
        //    {
        //        searchUserId = (from s in db.AspNetUsers
        //                        where s.UserName == UserName
        //                        select s.Id).FirstOrDefault();
        //    }
        //    //如果有存在UserId
        //    if(!String.IsNullOrEmpty(searchUserId))
        //    {
        //        //則將此UserId的所有訂單找出
        //        using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
        //        {
        //            var result = (from s in db.Orders
        //                          where s.UserId == searchUserId
        //                          select s).ToList();
        //            //回傳 結果 至Index()的View
        //            return View("Index", result);
        //        }
        //    }
        //    else
        //    {
        //        //回傳空結果
        //        return View("Index", new List<Models.Order>());
        //    }
        //}

        public ActionResult SearchByUserName(string SearchType, string SearchString)
        {
            //判斷為查詢使用者
            if (SearchType == "UserId")
            {                //儲存查詢出來的UserId
                string searchUserId = null;
                using (Models.UserEntities db = new Models.UserEntities())
                {
                    searchUserId = (from s in db.AspNetUsers
                                    where s.UserName == SearchString
                                    select s.Id).FirstOrDefault();
                }
                //如果有存在UserId
                if (!String.IsNullOrEmpty(searchUserId))
                {
                    //則將此UserId的所有訂單找出
                    using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
                    {
                        var result = (from s in db.Orders
                                      where s.UserId == searchUserId
                                      select s).ToList();
                        //回傳 結果 至Index()的View
                        return(View("Index", result));
                    }
                }
                else
                {
                    //回傳空結果
                    return(View("Index", new List <Models.Order>()));
                }
            }
            //判斷為查詢收件人電話
            else if (SearchType == "ReceiverPhone")
            {
                using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
                {
                    var result = (from s in db.Orders
                                  where s.ReceiverName == SearchString
                                  select s).ToList();
                    //回傳 結果 至Index()的View
                    return(View("Index", result));
                }
            }
            //判斷為查詢收件人地址
            else if (SearchType == "ReceiverAddress")
            {
                using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
                {
                    var result = (from s in db.Orders
                                  where s.ReceiverAddress == SearchString
                                  select s).ToList();
                    //回傳 結果 至Index()的View
                    return(View("Index", result));
                }
            }
            //判斷為查詢收件人名稱
            else if (SearchType == "ReceiverName")
            {
                using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities())
                {
                    var result = (from s in db.Orders
                                  where s.ReceiverPhone == SearchString
                                  select s).ToList();
                    //回傳 結果 至Index()的View
                    return(View("Index", result));
                }
            }
            else
            {
                //回傳空結果
                return(View("Index", new List <Models.Order>()));
            }
        }
Пример #13
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());
        }