コード例 #1
0
        public async Task <IActionResult> Update(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            Photo_Menu photo = await _context.Photo_Menus.FindAsync(id);

            if (photo == null)
            {
                return(NotFound());
            }
            return(View(photo));
        }
コード例 #2
0
        public async Task <IActionResult> DeletePost(int?id)
        {
            Photo_Menu photo = await _context.Photo_Menus.FindAsync(id);

            if (photo == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            Utility.DeleteImgFromFolder(_env.WebRootPath, photo.Image);

            _context.Photo_Menus.Remove(photo);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #3
0
        public async Task <IActionResult> Update(int?id, Photo_Menu photo)
        {
            Photo_Menu photoDb = await _context.Photo_Menus.FindAsync(id);

            if (photoDb == null)
            {
                return(RedirectToAction(nameof(Index)));
            }
            if (ModelState["Title"].ValidationState == ModelValidationState.Invalid
                )
            {
                return(View(photoDb));
            }

            if (photo.PhotoUpdate != null)
            {
                if (!photo.PhotoUpdate.IsImage())
                {
                    ModelState.AddModelError("Photo", "Content type must be image");
                    return(View());
                }

                if (!photo.PhotoUpdate.CheckImageSize(2))
                {
                    ModelState.AddModelError("Photo", "Image size not more than 2 Mb");
                    return(View());
                }

                string filename = await photo.PhotoUpdate.CopyImage(_env.WebRootPath, "slider");

                Utility.DeleteImgFromFolder(_env.WebRootPath, photoDb.Image);

                photoDb.Image = filename;
            }

            photoDb.Title = photo.Title;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }