コード例 #1
0
ファイル: OrderController.cs プロジェクト: apple13121/Shop
        /*public ActionResult Index(Models.OrderModel.Ship postback)
        {
            return View();
        }*/
        public ActionResult Index(Models.OrderModel.Ship postback)
        {
            if(this.ModelState.IsValid)
            {
                //取得目前的購物車
                var currentcart = Models.Cart.Operation.GetCurrentCart();

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

                using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
                {
                    //建立Order物件
                    var order = new Models.Cart.Order()
                    {
                        UserId = userId,
                        RecieverName = postback.RecieverName,
                        RecieverPhone = postback.RecieverPhone,
                        RecieverAddress = postback.RecieverAddress
                    };
                    //加入Orders資料表後,儲存變更
                    db.Orders.Add(order);
                    db.SaveChanges();

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

                    //將其加入OrderDetails資料表後,儲存變更
                    db.OrderDetails.AddRange(orderDetails);
                    db.SaveChanges();
                }
                return RedirectToAction("OrderSuccess");
            }
            return View();
        }
コード例 #2
0
ファイル: ProductController.cs プロジェクト: apple13121/Shop
        public ActionResult Edit(Models.Cart.Product postback)
        {
            if(this.ModelState.IsValid) //判斷使用者輸入資料是否正確
            {
                using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
                {
                    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);
            }
        }
コード例 #3
0
ファイル: HomeController.cs プロジェクト: apple13121/Shop
 public ActionResult Vegetable()
 {
     using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
     {
         var result = (from s in db.Products where s.CategoryId == 2 select s).ToList();
         return View(result);
     }
 }
コード例 #4
0
ファイル: HomeController.cs プロジェクト: apple13121/Shop
 public ActionResult Index()
 {
     using(Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
     {
         var result = (from s in db.Products select s).ToList();
         return View(result);
     }
 }
コード例 #5
0
 public ActionResult Index()
 {//取得Order中所有資料
     using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
     {
         var result = (from s in db.Orders
                       select s).ToList();
         return View(result);
     }
 }
コード例 #6
0
ファイル: OrderController.cs プロジェクト: apple13121/Shop
        public ActionResult MyOrder()
        {
            var userId = HttpContext.User.Identity.GetUserId();

            using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
            {
                var result = (from s in db.Orders
                              where s.UserId == userId
                              select s).ToList();

                return View(result);
            }
        }
コード例 #7
0
ファイル: ProductController.cs プロジェクト: apple13121/Shop
        public ActionResult Index()
        {
            //宣告<Product>result,等等要接db撈出來的資料
            List<Models.Cart.Product> result = new List<Models.Cart.Product>();

            //接收訊息
            ViewBag.ResultMessage = TempData["ResultMessage"];

            //DB把資料取出
            using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
            {
                result = (from s in db.Products select s).ToList();

                return View(result);
            }
        }
コード例 #8
0
ファイル: ProductController.cs プロジェクト: apple13121/Shop
 //編輯商品頁面(從資料庫中取出此資料資訊)
 public ActionResult Edit(int id)
 {
     using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
     {
         //抓取Product.Id 輸入id的資料
         var result = (from s in db.Products where s.Id == id select s).FirstOrDefault();
         if(result != default(Models.Cart.Product))//判斷此id是否有資料
         {
             return View(result);
         }
         else
         {
             TempData["ResultMessage"] = "資料有誤,請重新操作";
             return RedirectToAction("Index");
         }
     }
 }
コード例 #9
0
 public ActionResult Details(int id)
 {
     using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
     {
         //取得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);
         }
     }
 }
コード例 #10
0
ファイル: HomeController.cs プロジェクト: apple13121/Shop
        public ActionResult Detail(int id)
        {
            using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
            {
                var result = (from s in db.Products
                              where s.Id == id
                              select s).FirstOrDefault();

                if (result == default(Models.Cart.Product))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(result);
                }
            }
        }
コード例 #11
0
ファイル: OrderController.cs プロジェクト: apple13121/Shop
        public ActionResult MyOrderDetail(int id)
        {
            using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
            {
                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);
                }
            }
        }
コード例 #12
0
ファイル: ProductController.cs プロジェクト: apple13121/Shop
        public ActionResult Create(Models.Cart.Product postback)
        {
            if (this.ModelState.IsValid)//資料驗證成功
            {
                using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
                {
                    //回傳資料postback加入Products
                    db.Products.Add(postback);

                    //存取異動資料
                    db.SaveChanges();

                    //設定成功訊息
                    TempData["ResultMessage"] = String.Format("商品[{0}]成功建立", postback.Name);

                    return RedirectToAction("Index");
                }
            }
            //失敗訊息
            ViewBag.ResultMessage = "資料有誤請檢查";

            return View(postback);
        }
コード例 #13
0
ファイル: ProductController.cs プロジェクト: apple13121/Shop
        public ActionResult Delete(int id)
        {
            using (Models.Cart.CartDBContext db = new Models.Cart.CartDBContext())
            {
                //抓取product id
                var result = (from s in db.Products where s.Id == id select s).FirstOrDefault();
                if(result != default(Models.Cart.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");
                }
            }
        }