コード例 #1
0
        public ActionResult Edit(int id)
        {
            var result   = _product.GetInfo(id).CastTo <ProductModel>();
            var hashtags = new HashtagController(_tag).GetAllByProduct(id);

            result.Tags = new List <string>(hashtags.Select(_ => _.Name));

            var cat = GetAllCategories();

            ViewBag.Categories = new SelectList(cat, "Id", "Name", result != null ? result.CatId : 0);

            var origin = _origin.GetAll();

            ViewBag.Origin = new SelectList(origin, "Id", "Name", result != null ? result.OriginId : 0);

            var brand = _brand.GetAll();

            ViewBag.Brand = new SelectList(brand, "Id", "Name", result != null ? result.BrandId : 0);

            var             hashtag = new HashtagController(_tag).GetAll();
            MultiSelectList list    = new MultiSelectList(hashtag, "Name", "Name", hashtags.Select(_ => _.Name));

            ViewBag.Hashtag = list;

            ViewBag.Title = "Cập nhật sản phẩm";
            return(View("Add", result));
        }
コード例 #2
0
        public ActionResult Add()
        {
            var cat = GetAllCategories();

            ViewBag.Categories = new SelectList(cat, "Id", "Name");

            var origin = _origin.GetAll();

            ViewBag.Origin = new SelectList(origin, "Id", "Name");

            var brand = _brand.GetAll();

            ViewBag.Brand = new SelectList(brand, "Id", "Name");

            var hashtag = new HashtagController(_tag).GetAll();

            ViewBag.Hashtag = new MultiSelectList(hashtag, "Name", "Name");

            ViewBag.Title = "Thêm sản phẩm";
            return(View(new ProductModel()));
        }
コード例 #3
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));
        }