Ejemplo n.º 1
0
        public ActionResult Update(CategoryModel category, string fileCollections)
        {
            if (ModelState.IsValid)
            {
                List <FileModel> files = fileCollections.ConvertToFiles();

                if (files != null)
                {
                    FileModel file = files.Where(m => m.Selected).FirstOrDefault();

                    // if not have file which selected then system will pick the first file
                    if (file == null)
                    {
                        file = files[0];
                    }
                    category.Thumbnail = file.Name;
                    //Upload thumb image
                    var uploadFile = new ImageController(_image).FilesUpload(file, new Image
                    {
                        ProductId = -2,
                        Width     = WebConfigurations.ImageThumbSize.Width,
                        Height    = WebConfigurations.ImageThumbSize.Height
                    }, WebConfigurations.ThumbPath, string.Empty, true);

                    //Upload full zise
                    uploadFile = new ImageController(_image).FilesUpload(file, new Image
                    {
                        ProductId = -2,
                        Width     = WebConfigurations.ImageFullSize.Width,
                        Height    = WebConfigurations.ImageFullSize.Height
                    }, WebConfigurations.ImageFullPath, string.Empty);
                    if (!uploadFile)
                    {
                        return(View("Add", category));
                    }
                }

                Category cat = new Category()
                {
                    Id           = category.Id,
                    Name         = category.Name,
                    ParentId     = category.ParentId ?? 0,
                    Thumbnail    = category.Thumbnail,
                    ShowHomePage = category.ShowHomePage,
                    ShowMenu     = category.ShowMenu,
                    ActiveStatus = category.ActiveStatus,
                    Description  = category.Description,
                    SortOrder    = category.SortOrder ?? 0
                };
                var result = _category.Insert(cat, CurrentUserLogin);
                return(Redirect("management"));
            }
            return(View("Add", category));
        }
Ejemplo n.º 2
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));
        }