Пример #1
0
        public ActionResult Create([Bind(Include = "ImageID,UI,Picture")] ImageViewModel image, HttpPostedFileBase photo, int imgId)
        {
            if (ModelState.IsValid)
            {
                if (photo != null && photo.ContentLength > 0)
                {
                    var supportedTypes = new[] { "jpg", "jpeg", "png" };

                    var fileExt = System.IO.Path.GetExtension(photo.FileName).Substring(1);

                    if (!supportedTypes.Contains(fileExt))
                    {
                        ModelState.AddModelError("photo", "Invalid type. Only the following types (jpg, jpeg, png) are supported.");
                    }
                    image.Picture = new byte[photo.ContentLength];
                    photo.InputStream.Read(image.Picture, 0, image.Picture.Length);
                    image.ImageID = imgId;
                    _imageService.Edit(image);
                }
                if (_categoryService.GetByImg(imgId) != null)
                {
                    return(RedirectToAction("Index", "Categories"));
                }
                if (_subcategoryService.GetByImg(imgId) != null)
                {
                    return(RedirectToAction("IndexAdmin", "Subcategories", new { id = _subcategoryService.GetByImg(imgId).CategoryID }));
                }
                if (_makerService.GetByImg(imgId) != null)
                {
                    return(RedirectToAction("Index", "Makers"));
                }
                if (_productService.GetByImg(imgId) != null)
                {
                    return(RedirectToAction("Index", "Products", new { id = _productService.GetByImg(imgId).SubcategoryID }));
                }
            }
            return(View(image));
        }