示例#1
0
        public ActionResult AddNewProduct(string name, string[] sizeList, string[] colorList, string description,
                                          int categoryID, int supplierID, decimal price)
        {
            StringBuilder size  = new StringBuilder();
            StringBuilder color = new StringBuilder();

            for (int i = 0; i < sizeList.Length; i++)
            {
                size.Append(sizeList[i]);
                if (i < sizeList.Length - 1)
                {
                    size.Append(",");
                }
            }

            for (int i = 0; i < colorList.Length; i++)
            {
                color.Append(colorList[i]);
                if (i < colorList.Length - 1)
                {
                    color.Append(",");
                }
            }

            List <ProductImageViewModel> listProductImage = new List <ProductImageViewModel>();
            ProductImageViewModel        productImage     = new ProductImageViewModel();
            var orderCount = 0;

            byte[] avatarImage = null;
            if (Request.Files.Count > 0)
            {
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var file            = Request.Files[i];
                    int fileSizeInBytes = file.ContentLength;

                    using (var br = new BinaryReader(file.InputStream))
                    {
                        avatarImage = br.ReadBytes(fileSizeInBytes);
                    }
                    string picUrl = null;
                    if (avatarImage != null)
                    {
                        picUrl = SaveImageToServer(avatarImage);
                        productImage.PicUrl       = picUrl;
                        productImage.DisplayOrder = ++orderCount;
                        listProductImage.Add(productImage);
                    }
                }
            }

            ProductViewModel newProduct = new ProductViewModel()
            {
                Name          = name,
                Size          = size.ToString(),
                Color         = color.ToString(),
                Description   = description,
                CategoryID    = categoryID,
                SupplierId    = supplierID,
                Price         = price,
                ProductImages = listProductImage,
                Active        = true,
            };
            ProductApi productApi = new ProductApi();

            productApi.Create(newProduct);
            return(Json(new { success = true, message = "Successfully added!" }, JsonRequestBehavior.AllowGet));
        }