public EditProductPage()
        {
            this.InitializeComponent();
            var vm = new EditProductPageViewModel(new NavigationService());

            this.DataContext = vm;
        }
Exemple #2
0
        public ActionResult Edit(long id)
        {
            var result       = new EditProductPageViewModel();
            var groups       = _shopService.GetGroups().OrderBy(q => q.Name);
            var manufactures = _shopService.GetManufacturers().OrderBy(q => q.Name);

            if (id == 0)
            {
                var firstGroup                  = groups.First(q => q.Category.Any());
                var categoriesByFirstGroup      = _shopService.GetCategories(firstGroup.ID).ToList();
                var firstCategoriesByFirstGroup = categoriesByFirstGroup.First();
                result.Groups        = new SelectList(groups.OrderBy(q => q.Name), "ID", "Name", firstGroup);
                result.Manufacturers = new SelectList(manufactures, "ID", "Name");
                result.Categories    = new SelectList(categoriesByFirstGroup.OrderBy(q => q.Name), "ID", "Name",
                                                      firstCategoriesByFirstGroup);
                result.Sections = _productService.GetProductParameters(firstCategoriesByFirstGroup.ID).ToList();
                return(View(result));
            }

            var product = _productService.GetProduct(id);

            if (product == null)
            {
                return(RedirectToAction("Index"));
            }

            result.Product       = product;
            result.Groups        = new SelectList(groups.OrderBy(q => q.Name), "ID", "Name", product.Category.GroupID);
            result.Manufacturers = new SelectList(manufactures, "ID", "Name", product.ManufacturerID);
            result.Categories    = new SelectList(
                _shopService.GetCategories(product.Category.GroupID).OrderBy(q => q.Name), "ID", "Name",
                product.CategoryID);
            result.Sections = _productService.GetProductParameters(product.CategoryID, product.ID).ToList();

            //result.Photos =
            //    product.ProductImage.Select(
            //        q =>
            //            String.Format(
            //                "<img src='/Images/Catalog/Product/{0}' class='file-preview-image' alt='The Moon' title='The Moon'>",
            //                q.ImageName)).ToArray();

            ViewBag.UploadedImages = (product.ProductImage != null) ? from image in product.ProductImage select "/Images/Catalog/Product/" + image.ImageName + ";" + image.IsTitular.ToString().ToLower() : null;

            return(View(result));
        }
        public IActionResult Edit(string id)
        {
            var product = this.productsService.GetProductForCart(id);

            if (product == null)
            {
                this.TempData["Error"] = "Product does not exits.";
                return(RedirectToAction(nameof(Index)));
            }

            var model = new EditProductPageViewModel
            {
                Product       = product.Map <Product, EditProductViewModel>(),
                AllCategories = this.categoriesService.GetCategories()
                                .Select(c => c.Map <Category, CategoryListItemViewModel>()).ToList()
            };

            this.TempData["EditedProductId"] = id;
            return(View(model));
        }
        public async Task <IActionResult> Edit(EditProductPageViewModel model, string id, string submit)
        {
            var check = Guid.TryParse(id, out Guid parsedId);

            if (!check)
            {
                this.TempData["Error"] = "Product does not exits.";
                return(RedirectToAction(nameof(Index), "Products"));
            }

            var idCheck = this.TempData["EditedProductId"].ToString();

            if (idCheck != id)
            {
                this.TempData["Error"] = "Invalid operation.";
                return(RedirectToAction(nameof(Index)));
            }

            var categories = this.categoriesService.GetCategories()
                             .Select(c => c.Map <Category, CategoryListItemViewModel>())
                             .ToList();

            if (!ModelState.IsValid)
            {
                model.AllCategories = categories;

                this.TempData["EditedProductId"] = id;
                return(View(model));
            }

            var urls = new List <string>();

            if (model.ImagesFiles != null && model.ImagesFiles.Any())
            {
                foreach (var formFile in model.ImagesFiles)
                {
                    var uploadParams = new ImageUploadParams
                    {
                        File           = new FileDescription(formFile.FileName, formFile.OpenReadStream()),
                        Transformation = new Transformation().Width(600).Height(600).Crop("scale")
                    };

                    var result = await cloudinary.UploadAsync(uploadParams);

                    urls.Add(result.Uri.ToString());
                }
            }

            var type = categories.Where(c => c.Id == model.Product.CategoryId.ToString()).Select(c => c.Type)
                       .FirstOrDefault();

            var product = model.Product.Map <EditProductViewModel, Product>();

            product.Id = parsedId;

            var isSuccess = await this.productsService.EditProduct(product, this.User.Identity.Name, urls);

            if (!isSuccess)
            {
                this.TempData["Error"] = "Invalid operation.";
                return(RedirectToAction(nameof(Index)));
            }

            this.TempData["Success"] = $"Product {product.Name} edited successfully.";
            if (!string.IsNullOrEmpty(submit))
            {
                return(RedirectToAction(nameof(MyProducts)));
            }

            return(RedirectToAction(nameof(SizesController.Create), "Sizes", new { productId = product.Id, type, sex = model.Product.Sex }));
        }