Exemplo n.º 1
0
        public async Task <ActionResult> PictureCreate([FromForm] GalleryCreateViewModel galleryVM)
        {
            if (ModelState.IsValid)
            {
                var uploadedImage = "";
                if (galleryVM.Photo != null)
                {
                    uploadedImage = await ProcessPhoto(galleryVM.Photo);
                }

                try
                {
                    var picture = _mapper.Map <Picture>(galleryVM);
                    picture.CreatedAt = DateTime.Now;
                    picture.ImgUrl    = uploadedImage;

                    if (await _galleryRepo.AddPicture(picture))
                    {
                        TempData["Alert"] = "Picture Added Successfully";
                        return(RedirectToAction(nameof(GalleryIndex)));
                    }

                    ViewBag.Error = "Unable to add picture, please try again or contact administrator";
                    return(View());
                }
                catch (Exception ex)
                {
                    ViewBag.Error = "Unable to add picture, please try again or contact administrator";
                    return(View());
                }
            }
            ViewBag.Error = "Please correct the error(s) in Form";
            return(View(galleryVM));
        }