Ejemplo n.º 1
0
        public ActionResult Add(ProductViewModel product)
        {
            var app = new ServiceReference.ContractClient();

            var model = new ServiceReference.Product
            {
                Id          = 0,
                Name        = product.Name,
                Description = product.Description,
                Enabled     = true,
                IsOffer     = product.IsOffer,
                Percent     = product.Percent,
                Price       = product.Price,
                StartDay    = product.StartDay,
                EndDay      = product.EndDay,
                CategoryId  = product.CategoryId,
                Warranty    = product.Warranty,
                Brant       = product.Brant,
                TypeStock   = product.TypeStock,
                Stock       = product.Stock,
            };

            model.Feature = product.Feature.Where(x => x.Id >= 0 && x.Name != null).Select(x => new ServiceReference.Feature
            {
                Description = x.Description,
                Name        = x.Name,
                Id          = x.Id,
                ProductID   = model.Id
            }).ToList();
            model.Image = new List <ServiceReference.Image>();

            var id = app.AddProduct(model);


            for (int i = 0; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];
                if (file.FileName == string.Empty)
                {
                    continue;
                }
                var path = ImageHelper.SaveImage(id.ToString(), file, Server.MapPath("~/Image"));


                bool isMain = false;
                if (Request.Files.AllKeys[i] == "file")
                {
                    isMain = true;
                }
                var imagetosave = new ServiceReference.Image
                {
                    Url       = path,
                    IsMain    = isMain,
                    ProductId = id
                };
                model.Image.Add(imagetosave);
            }
            app.AddImageRange(model.Image);


            return(RedirectToAction("GetAll"));
        }