Esempio n. 1
0
        public ActionResult Create(Order order, int[] productIDs, int[] quantities)
        {
            //Kiểm tra hợp lệ dữ liệu
            if (order.OrderDate == new DateTime(0001, 01, 01))
            {
                ModelState.AddModelError("OrderDate", "OrderDate is invalid");
            }
            if (string.IsNullOrEmpty(order.CustomerID))
            {
                ModelState.AddModelError("CustomerID", "Please select a customer");
            }
            if (order.EmployeeID == 0)
            {
                ModelState.AddModelError("EmployeeID", "Please select an Employee");
            }
            if (order.RequiredDate == new DateTime(0001, 01, 01))
            {
                ModelState.AddModelError("RequiredDate", "RequiredDate is invalid");
            }
            if (string.IsNullOrEmpty(order.ShipAddress))
            {
                ModelState.AddModelError("ShipAddress", "Please enter ship address");
            }
            if (string.IsNullOrEmpty(order.ShipCity))
            {
                ModelState.AddModelError("ShipCity", "Please enter ship city");
            }
            if (string.IsNullOrEmpty(order.ShipCountry))
            {
                ModelState.AddModelError("ShipCountry", "Please enter ship country");
            }
            if (order.ShippedDate == new DateTime(0001, 01, 01))
            {
                ModelState.AddModelError("ShippedDate", "ShippedDate is invalid");
            }
            if (order.ShipperID == 0)
            {
                ModelState.AddModelError("ShipperID", "Please select Shipper");
            }
            if (productIDs != null)
            {
                try
                {
                    for (int i = 0; i < productIDs.Length; i++)
                    {
                        int temp = Convert.ToInt32(productIDs[i]);
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Quantity", "Quantity(s) must be a number");
                }
            }

            if (ModelState.IsValid)
            {
                //Lưu vào DB
                if (order.OrderID == 0)
                {
                    //Tạo mới
                    int orderID = OrderBLL.AddOrder(order);
                    if (productIDs != null && quantities != null)
                    {
                        int detailLenght = productIDs.Length;
                        for (int i = 0; i < detailLenght; i++)
                        {
                            if (productIDs[i] <= 0 || quantities[i] <= 0)
                            {
                                continue;
                            }
                            else
                            {
                                OrderBLL.AddOrderDetail(new OrderDetail() { OrderID = orderID, ProductID = productIDs[i], Quantity = quantities[i] });
                            }
                        }
                    }
                }
                else
                {
                    //Sửa
                    OrderBLL.Update(order);
                    OrderBLL.DeleteOrderDetails(order.OrderID);
                    if (productIDs != null && quantities != null)
                    {
                        int detailLenght = productIDs.Length;
                        for (int i = 0; i < detailLenght; i++)
                        {
                            if (productIDs[i] <= 0 || quantities[i] <= 0)
                            {
                                continue;
                            }
                            else
                            {
                                OrderBLL.AddOrderDetail(new OrderDetail() { OrderID = order.OrderID, ProductID = productIDs[i], Quantity = quantities[i] });
                            }
                        }
                    }
                }
                return RedirectToAction("Index");
            }
            else
            {
                order.Details = OrderBLL.ListOfOrderDetail(order.OrderID);
                if (order.Details == null)
                {
                    order.Details = new List<OrderDetail>();
                }
                return View(order);
            }
        }