Example #1
0
        public ActionResult InputAttributes(ProductAttribute model)
        {
            if (string.IsNullOrEmpty(model.AttributeName))
            {
                ModelState.AddModelError("AttributeName", "AttributeName Expected");
            }
            if (string.IsNullOrEmpty(model.AttributeValues))
            {
                ModelState.AddModelError("AttributeValues", "AttributeValues Expected");
            }
            if (model.DisplayOrder == 0)
            {
                model.DisplayOrder = 0;
            }



            if (!ModelState.IsValid)
            {
                ViewBag.Title = model.AttributeID == 0 ? "Create New ProductAttribute" : "Edit a ProductAttribute";
                return(View(model));
            }

            if (model.AttributeID == 0)
            {
                CatalogBLL.AddProductAttribute(model);
            }
            else
            {
                CatalogBLL.UpdateProductAttribute(model);
            }
            return(RedirectToAction("Index"));
        }
        public ActionResult Attribute(ProductAttribute model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.AttributeValues))
                {
                    ModelState.AddModelError("AttributeValues", "AttributeValues expected!");
                }
                if (string.IsNullOrEmpty(model.AttributeName))
                {
                    ModelState.AddModelError("AttributeName", "AttributeName expected!");
                }

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

                //TODO: Lưu dữ liệu vào DB
                if (model.AttributeID == 0)
                {
                    CatalogBLL.AddProductAttribute(model);
                }
                else
                {
                    CatalogBLL.UpdateProductAttribute(model);
                }
                return(RedirectToAction("Input/" + Convert.ToString(model.ProductID)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace);
                return(View(model));
            }
        }
Example #3
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));
        }
        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));
                }
            }
        }