Ejemplo n.º 1
0
        public ActionResult Input(Product model, HttpPostedFileBase uploadFile)
        {
            if (string.IsNullOrEmpty(model.ProductName))
            {
                ModelState.AddModelError("ProductName", "ProductName Expected");
            }
            if (model.CategoryID.ToString() == "0")
            {
                ModelState.AddModelError("CategoryID", "CategoryName Expected");
            }
            if (model.SupplierID.ToString() == "0")
            {
                ModelState.AddModelError("SupplierID", "SupplierName Expected");
            }
            if (model.UnitPrice == 0)
            {
                ModelState.AddModelError("UnitPrice", "UnitPrice Expected");
            }
            if (string.IsNullOrEmpty(model.QuantityPerUnit))
            {
                model.QuantityPerUnit = "";
            }
            if (string.IsNullOrEmpty(model.Descriptions))
            {
                model.Descriptions = "";
            }

            if (uploadFile != null)
            {
                string folder   = Server.MapPath("~/Images/Uploads/Products");
                string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}";
                //  string fileName = Guid.NewGuid() + uploadFile.FileName;
                string filePath = Path.Combine(folder, fileName);
                uploadFile.SaveAs(filePath);
                model.PhotoPath = fileName;
            }
            if (!ModelState.IsValid)
            {
                ViewBag.Title = model.ProductID == 0 ? "Create new Product" : "Edit a Product";
                return(View(model));
            }

            if (model.ProductID == 0)
            {
                CatalogBLL.AddProduct(model);
            }
            else
            {
                CatalogBLL.UpdateProduct(model);
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// tao moi hoac sua
        /// </summary>
        /// <param name="model"></param>
        /// <param name="atts"></param>
        /// <param name="uploadFile"></param>
        /// <returns></returns>
        public ActionResult Input(Product model, string[] atts, HttpPostedFileBase uploadFile)
        {
            try
            {
                if (model.CategoryID == 0)
                {
                    ModelState.AddModelError("CategoryID", "Please select a Category");
                }
                if (model.SupplierID == 0)
                {
                    ModelState.AddModelError("SupplierID", "Please select a Supplier");
                }
                if (model.Description == null)
                {
                    model.Description = "";
                }
                if (string.IsNullOrEmpty(model.PhotoPath))
                {
                    Product existProduct = CatalogBLL.GetProduct(model.ProductID);
                    if (existProduct != null)
                    {
                        model.PhotoPath = existProduct.PhotoPath;
                    }
                    else
                    {
                        model.PhotoPath = "";
                    }
                }
                if (string.IsNullOrEmpty(model.ProductName))
                {
                    ModelState.AddModelError("ProductName", "Product Name is invalid");
                }
                if (string.IsNullOrEmpty(model.QuantityPerUnit))
                {
                    ModelState.AddModelError("QuantityPerUnit", "Quantity Per Unit is invalid");
                }
                if (model.UnitPrice == 0)
                {
                    ModelState.AddModelError("UnitPrice", "Unit Price is invalid");
                }

                //Upload anh
                if (uploadFile != null)
                {
                    string _FileName = Path.GetFileName(uploadFile.FileName);
                    _FileName = DateTime.Now.Ticks.ToString() + Path.GetExtension(_FileName);
                    string _path = Path.Combine(Server.MapPath("~/Uploads/Images"), _FileName);
                    uploadFile.SaveAs(_path);
                    model.PhotoPath = _FileName;
                }
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                //if (ModelState.IsValid)
                //{
                //Luu vao DB
                if (model.ProductID == 0)
                {
                    int productID = CatalogBLL.AddProduct(model);
                    if (atts != null)
                    {
                        List <ProductAttribute> productAttributes = new List <ProductAttribute>();
                        int attLen = atts.Count();
                        for (int i = 0; i < attLen; i++)
                        {
                            switch (i)
                            {
                            case 0:
                                productAttributes.Add(new ProductAttribute()
                                {
                                    AttributeID = 1, AttributeName = "Size", AttributeValues = atts[i], DisplayOrder = 1, ProductID = productID
                                });
                                break;

                            case 1:
                                productAttributes.Add(new ProductAttribute()
                                {
                                    AttributeID = 2, AttributeName = "Color", AttributeValues = atts[i], DisplayOrder = 1, ProductID = productID
                                });
                                break;

                            case 2:
                                productAttributes.Add(new ProductAttribute()
                                {
                                    AttributeID = 3, AttributeName = "Material", AttributeValues = atts[i], DisplayOrder = 1, ProductID = productID
                                });
                                break;

                            case 3:
                                productAttributes.Add(new ProductAttribute()
                                {
                                    AttributeID = 4, AttributeName = "Origin", AttributeValues = atts[i], DisplayOrder = 1, ProductID = productID
                                });
                                break;

                            default:
                                break;
                            }
                        }
                        CatalogBLL.AddProductAttribute(productAttributes);
                    }
                }
                else
                {
                    CatalogBLL.UpdateProduct(model);
                    if (atts != null)
                    {
                        List <ProductAttribute> productAttributes = new List <ProductAttribute>();
                        int attLen = atts.Length;
                        for (int i = 0; i < attLen; i++)
                        {
                            switch (i)
                            {
                            case 0:
                                productAttributes.Add(new ProductAttribute()
                                {
                                    AttributeID = 1, AttributeName = "Size", AttributeValues = atts[i], DisplayOrder = 1, ProductID = model.ProductID
                                });
                                break;

                            case 1:
                                productAttributes.Add(new ProductAttribute()
                                {
                                    AttributeID = 2, AttributeName = "Color", AttributeValues = atts[i], DisplayOrder = 1, ProductID = model.ProductID
                                });
                                break;

                            case 2:
                                productAttributes.Add(new ProductAttribute()
                                {
                                    AttributeID = 3, AttributeName = "Material", AttributeValues = atts[i], DisplayOrder = 1, ProductID = model.ProductID
                                });
                                break;

                            case 3:
                                productAttributes.Add(new ProductAttribute()
                                {
                                    AttributeID = 4, AttributeName = "Origin", AttributeValues = atts[i], DisplayOrder = 1, ProductID = model.ProductID
                                });
                                break;

                            default:
                                break;
                            }
                        }
                        CatalogBLL.UpdateProductAttribute(productAttributes);
                    }
                }
                return(RedirectToAction("Index"));
                //}
            }
            catch (Exception ex)
            {
                return(Content(ex.Message + ": " + ex.StackTrace));
            }
            return(View(model));
        }
Ejemplo n.º 3
0
        public ActionResult Input(Product data, HttpPostedFileBase uploadFile)
        {
            //Kiểm tra dữ liệu đầu vào
            if (data.CategoryID.ToString() == "0")
            {
                ModelState.AddModelError("CategoryID", "*");
            }

            if (data.SupplierID.ToString() == "0")
            {
                ModelState.AddModelError("SupplierID", "*");
            }

            if (string.IsNullOrEmpty(data.ProductName))
            {
                ModelState.AddModelError("ProductName", "**");
            }
            if (string.IsNullOrEmpty(data.QuantityPerUnit))
            {
                ModelState.AddModelError("QuantityPerUnit", "**");
            }

            //UnitPrice kiểu decimal
            if (string.IsNullOrEmpty(data.UnitPrice.ToString()))
            {
                ModelState.AddModelError("UnitPrice", "**");
            }
            if (string.IsNullOrEmpty(data.PhotoPath))
            {
                data.PhotoPath = "";
            }
            if (string.IsNullOrEmpty(data.Descriptions))
            {
                data.Descriptions = "";
            }
            //Nếu nó chưa thành công thì ở lại trang hiện tại
            if (!ModelState.IsValid)
            {
                if (data.ProductID == 0)
                {
                    ViewBag.Title = "Create new a Product";
                    SetAlert("Add new Product Fail , Check again!", "warning");
                }

                else
                {
                    ViewBag.Title = "Update a Product";
                    SetAlert("Update Product Fail , Check again!", "warning");
                }

                return(View(data));
            }
            //: new update
            if (uploadFile != null)
            {
                //Tạo folder để upload ảnh lên Server
                string folder = Server.MapPath("~/Images/ProductUpload");

                // string fileName = uploadFile.FileName;  => dễ bị trùng khi client chọn lại
                // => Solution : Sinh fileName theo giây phút để tránh trùng lặp
                //c1 : Cách ghép chuổi fileName
                string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}";
                //cach 2
                // string fileName1 = string.Format("{0}{1}" ,DateTime.Now.Ticks,Path.GetExtension(uploadFile.FileName));

                string filePath = Path.Combine(folder, fileName);

                //Upload ảnh lên Server
                uploadFile.SaveAs(filePath);

                //  return content|redirectoaction()
                // return Json (model, JsonRequestBehavior.AllowGet);

                //Gán dữ liệu cho PhotoPath
                data.PhotoPath = fileName;
            }

            if (data.ProductID == 0)
            {
                CatalogBLL.AddProduct(data);
                SetAlert("Add new Product success !", "success");
            }
            else
            {
                CatalogBLL.UpdateProduct(data);
                SetAlert("Update Product success !", "success");
            }



            return(RedirectToAction("Index"));
        }
Ejemplo n.º 4
0
        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));
            }
        }
Ejemplo n.º 5
0
        public IActionResult Input(ProductPostRequest model, ProductAttribute attribute, List <ProductAttribute> lstAttribute, string id = "", string query = "")
        {
            if (query != null && query == "AddAttribute")
            {
                // Attribute POST
                CatalogBLL.AddProductAttribute(attribute);
                return(RedirectToAction("Input", new { id = id }));
            }
            else
            {
                // Product POST
                try
                {
                    CheckNotNull(model);
                    SetEmptyNullableField(model);

                    string photoPath = UploadedFile(model);
                    photoPath = string.IsNullOrEmpty(id)
                        ? ""
                        : string.IsNullOrEmpty(photoPath)
                            ? CatalogBLL.GetProduct(Convert.ToInt32(id)).PhotoPath
                            : photoPath;

                    Product newProduct = new Product()
                    {
                        ProductID       = model.ProductID,
                        ProductName     = model.ProductName,
                        SupplierID      = model.SupplierID,
                        CategoryID      = model.CategoryID,
                        QuantityPerUnit = model.QuantityPerUnit,
                        UnitPrice       = model.UnitPrice,
                        Descriptions    = model.Descriptions,
                        PhotoPath       = photoPath
                    };

                    // Save data into DB
                    if (newProduct.ProductID == 0)
                    {
                        CatalogBLL.AddProduct(newProduct);
                    }
                    else
                    {
                        CatalogBLL.UpdateProduct(newProduct);
                    }
                    return(RedirectToAction("Index"));
                }
                catch (MissingFieldException)
                {
                    Product errorProduct = new Product()
                    {
                        ProductID       = model.ProductID,
                        ProductName     = model.ProductName,
                        SupplierID      = model.SupplierID,
                        CategoryID      = model.CategoryID,
                        QuantityPerUnit = model.QuantityPerUnit,
                        UnitPrice       = model.UnitPrice,
                        Descriptions    = model.Descriptions,
                    };
                    return(View(errorProduct));
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message + ": " + ex.StackTrace);
                    return(View(model));
                }
            }
        }
Ejemplo n.º 6
0
        public ActionResult Input(Product model, HttpPostedFileBase uploadFile, string method)
        {
            ProductAttributeResult models = new 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);
                    if (!extensionName.Equals(".jpg") && !extensionName.Equals(".JPG") && !extensionName.Equals(".png") && !extensionName.Equals(".PNG"))
                    {
                        ModelState.AddModelError("ExtensionName", "File type expected!");
                    }
                    else
                    {
                        // 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 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 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));
            }
        }
 public ActionResult Input(Product model, HttpPostedFileBase PhotoPath, string PhotoPathDraft, string Type = "")
 {
     try
     {
         //TODO :Kiểm tra tính hợp lệ của dữ liệu nhập vào
         if (string.IsNullOrEmpty(model.ProductName))
         {
             ModelState.AddModelError("ProductName", "ProductName expected");
         }
         if (model.CategoryID == 0)
         {
             ModelState.AddModelError("CategoryID", "CategoryID expected");
         }
         if (model.SupplierID == 0)
         {
             ModelState.AddModelError("SupplierID", "SupplierID expected");
         }
         if (string.IsNullOrEmpty(model.QuantityPerUnit))
         {
             ModelState.AddModelError("QuantityPerUnit", "QuantityPerUnit expected");
         }
         if (string.IsNullOrEmpty(model.PhotoPath))
         {
             ModelState.AddModelError("PhotoPath", "PhotoPath expected");
         }
         if (model.UnitPrice < 0)
         {
             ModelState.AddModelError("UnitPrice", "UnitPrice expected");
         }
         if (string.IsNullOrEmpty(model.Descriptions))
         {
             model.Descriptions = "";
         }
         if (PhotoPath != null)
         {
             string FileName = $"{DateTime.Now.Ticks}{Path.GetExtension(PhotoPath.FileName)}";
             string path     = Path.Combine(Server.MapPath("~/Images/products"), FileName);
             PhotoPath.SaveAs(path);
             model.PhotoPath = FileName;
         }
         //TODO :Lưu dữ liệu nhập vào
         if (model.ProductID == 0)
         {
             int productID = CatalogBLL.AddProduct(model);
             return(RedirectToAction("Attribute", new { CategoryID = model.CategoryID, ProductID = productID, Type = Type }));
         }
         else
         {
             if (string.IsNullOrEmpty(model.PhotoPath))
             {
                 model.PhotoPath = PhotoPathDraft;
             }
             CatalogBLL.UpdateProduct(model);
         }
         return(RedirectToAction("Index"));
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace);
         return(View(model));
     }
 }