예제 #1
0
        public ActionResult Update(ProductModel model, string fileCollections)
        {
            if (ModelState.IsValid)
            {
                Product product   = model.CastTo <Product>();
                string  signature = product.Signature;
                string  tags      = product.Tags == null ? string.Empty : string.Join(",", product.Tags);

                string           fileNameExt = DateTime.Now.Ticks.ToStringToDefault().ToUnSign() + "_";
                List <FileModel> files       = fileCollections.ConvertToFiles();
                //Set thumbnail for product
                if (files != null)
                {
                    FileModel file = files.Where(m => m.Selected).FirstOrDefault();
                    if (file == null)
                    {
                        //If not select a file, system will pick the first item.
                        file = files[0];
                    }

                    string fileName = (file.Base64.IsNotNullOrEmpty() ? fileNameExt : "") + file.Name;
                    product.Thumbnail = fileName;
                }
                // Insert product: output is product info
                product = _product.InsertProduct(product, CurrentUserLogin);
                // Insert tags of product
                _tag.UpdateTagByProduct(product.Id, tags);


                //Delete all product image before add new images.
                var status = _image.DeleteImageByProduct(product.Id);
                //Upload image for product
                //Upload thumb image
                var uploadFiles = new ImageController(_image).FilesUpload(files, new Image
                {
                    ProductId = product.Id,
                    Signature = signature,
                    Width     = WebConfigurations.ImageThumbSize.Width,
                    Height    = WebConfigurations.ImageThumbSize.Height
                }, WebConfigurations.ThumbPath, fileNameExt, true);

                //Upload full size
                uploadFiles = new ImageController(_image).FilesUpload(files, new Image
                {
                    ProductId = product.Id,
                    Signature = signature,
                    Width     = WebConfigurations.ImageFullSize.Width,
                    Height    = WebConfigurations.ImageFullSize.Height
                }, WebConfigurations.ImageFullPath, fileNameExt);

                //If upload file is successful then return to current page else to management page.
                if (!uploadFiles)
                {
                    var cat = GetAllCategories();
                    ViewBag.Categories = new SelectList(cat, "Id", "Name", product != null ? product.CatId : 0);
                    var             hashtag = new HashtagController(_tag).GetAll();
                    MultiSelectList list    = new MultiSelectList(hashtag, "Name", "Name", tags);
                    ViewBag.Hashtag    = list;
                    TempData["result"] = "Lỗi trong quá trình upload hình ảnh.";
                    return(View("Add", model));
                }
                return(Redirect("management"));
            }
            return(View("Add", model));
        }