public ActionResult Input(string id = "")
 {
     try
     {
         if (string.IsNullOrEmpty(id))
         {
             ViewBag.Title  = "Create new product";
             ViewBag.Method = "Add";
             Product newProduct = new Product()
             {
                 ProductID = 0
             };
             Models.ProductAttributeResult model = new Models.ProductAttributeResult()
             {
                 Product           = newProduct,
                 ProductAttributes = null
             };
             return(View(model));
         }
         else
         {
             ViewBag.Title  = "Edit a product";
             ViewBag.Method = "Edit";
             Product editProduct = CatalogBLL.GetProduct(Convert.ToInt32(id));
             List <ProductAttribute>       attr  = CatalogBLL.ListOfProductAttribute(Convert.ToInt32(id));
             Models.ProductAttributeResult model = new Models.ProductAttributeResult()
             {
                 Product           = editProduct,
                 ProductAttributes = attr
             };
             if (editProduct == null)
             {
                 return(RedirectToAction("Index"));
             }
             return(View(model));
         }
     }
     catch (Exception ex)
     {
         return(Content(ex.Message + ": " + ex.StackTrace));
     }
 }
        public ActionResult Input(Product model, HttpPostedFileBase uploadFile)
        {
            Models.ProductAttributeResult models = new Models.ProductAttributeResult();
            try
            {
                if (string.IsNullOrEmpty(model.ProductName))
                {
                    ModelState.AddModelError("ProductName", "ProductName expected");
                }
                if ((model.SupplierID == 0))
                {
                    ModelState.AddModelError("SupplierID", "SupplierID expected");
                }
                if (model.CategoryID == 0)
                {
                    ModelState.AddModelError("CategoryID", "CategoryID expected");
                }
                if (string.IsNullOrEmpty(model.QuantityPerUnit))
                {
                    ModelState.AddModelError("QuantityPerUnit", "QuantityPerUnit expected");
                }
                if (model.UnitPrice <= 0)
                {
                    ModelState.AddModelError("UnitPrice", "UnitPrice expected");
                }
                if (string.IsNullOrEmpty(model.Descriptions))
                {
                    ModelState.AddModelError("Descriptions", "Descriptions expected");
                }
                if (string.IsNullOrEmpty(model.PhotoPath))
                {
                    model.PhotoPath = "";
                }

                if (uploadFile != null)
                {
                    string folder = Server.MapPath("../Images/Uploads");
                    //string fileName = uploadfile.FileName;
                    string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}";
                    string filePath = Path.Combine(folder, fileName);
                    uploadFile.SaveAs(filePath);
                    model.PhotoPath = fileName;
                }

                if (ViewBag.Method == "Add")
                {
                    ViewBag.Title  = "Create new product";
                    ViewBag.Method = "Add";
                    Product newProduct = new Product()
                    {
                        ProductID = 0
                    };
                    models = new Models.ProductAttributeResult()
                    {
                        Product           = newProduct,
                        ProductAttributes = null
                    };
                }
                else if (ViewBag.Method == "Edit")
                {
                    ViewBag.Title  = "Edit a product";
                    ViewBag.Method = "Edit";
                    Product editProduct          = CatalogBLL.GetProduct(Convert.ToInt32(model.ProductID));
                    List <ProductAttribute> attr = CatalogBLL.ListOfProductAttribute(Convert.ToInt32(model.ProductID));
                    models = new Models.ProductAttributeResult()
                    {
                        Product           = editProduct,
                        ProductAttributes = attr
                    };
                }

                if (!ModelState.IsValid)
                {
                    return(View(models));
                }

                if (model.ProductID == 0)
                {
                    CatalogBLL.AddProduct(model);
                }
                else
                {
                    CatalogBLL.UpdateProduct(model);
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace);
                return(View(model));
            }
        }
Esempio n. 3
0
        public ActionResult Input(Product model, HttpPostedFileBase uploadFile, string method)
        {
            Models.ProductAttributeResult models = new Models.ProductAttributeResult();
            try
            {
                //TODO: Kiểm tra tính hợp lệ của dữ liệu
                if (string.IsNullOrEmpty(model.ProductName))
                {
                    ModelState.AddModelError("ProductName", "ProductName expected!");
                }
                if (string.IsNullOrEmpty(model.QuantityPerUnit))
                {
                    ModelState.AddModelError("QuantityPerUnit", "QuantityPerUnit expected!");
                }
                if (double.IsNaN(model.UnitPrice))
                {
                    ModelState.AddModelError("UnitPrice", "UnitPrice expected!");
                }
                if (model.SupplierID == 0)
                {
                    ModelState.AddModelError("SupplierID", "SupplierID expected!");
                }
                if (model.CategoryID == 0)
                {
                    ModelState.AddModelError("CategoryID", "CategoryID expected!");
                }
                if (string.IsNullOrEmpty(model.Descriptions))
                {
                    model.Descriptions = "";
                }
                if (string.IsNullOrEmpty(model.PhotoPath))
                {
                    model.PhotoPath = "";
                }

                //Upload ảnh
                if (uploadFile != null)
                {
                    string folder = Server.MapPath("~/Images/Uploads");
                    //Lấy phần mở rộng của file
                    string extensionName = System.IO.Path.GetExtension(uploadFile.FileName);
                    // Tạo tên file ngẫu nhiên
                    string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}";
                    string filePath = Path.Combine(folder, fileName);
                    uploadFile.SaveAs(filePath);

                    model.PhotoPath = fileName;
                }

                if (method == "Add")
                {
                    ViewBag.Title  = "Create new product";
                    ViewBag.Method = "Add";
                    Product newProduct = new Product()
                    {
                        ProductID = 0
                    };
                    models = new Models.ProductAttributeResult()
                    {
                        Product           = newProduct,
                        ProductAttributes = null
                    };
                }
                else if (method == "Edit")
                {
                    ViewBag.Title  = "Edit a product";
                    ViewBag.Method = "Edit";
                    Product editProduct          = CatalogBLL.GetProduct(Convert.ToInt32(model.ProductID));
                    List <ProductAttribute> attr = CatalogBLL.ListOfProductAttribute(Convert.ToInt32(model.ProductID));
                    models = new Models.ProductAttributeResult()
                    {
                        Product           = editProduct,
                        ProductAttributes = attr
                    };
                }

                if (!ModelState.IsValid)
                {
                    return(View(models));
                }

                //TODO: Lưu dữ liệu vào DB
                if (model.ProductID == 0)
                {
                    CatalogBLL.AddProduct(model);
                }
                else
                {
                    CatalogBLL.UpdateProduct(model);
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace);
                return(View(models));
            }
        }