コード例 #1
0
        public async Task <IActionResult> Create(Enjoy enjoy)
        {
            if (ModelState["Title"].ValidationState == ModelValidationState.Invalid ||
                ModelState["Photo"].ValidationState == ModelValidationState.Invalid)
            {
                return(View());
            }

            if (!enjoy.Photo.IsImage())
            {
                ModelState.AddModelError("Photo", "Shekil novunu duz secin");
                return(View());
            }
            if (!enjoy.Photo.CheckImageSize(2))
            {
                ModelState.AddModelError("Photo", "Shekil hecmi coxdur");
                return(View());
            }


            string filename = await enjoy.Photo.CopyImage(_env.WebRootPath, "slider");

            enjoy.Image = filename;
            await _context.Enjoys.AddAsync(enjoy);

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        public async Task <IActionResult> Update(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            Enjoy enjoy = await _context.Enjoys.FindAsync(id);

            if (enjoy == null)
            {
                return(NotFound());
            }
            return(View(enjoy));
        }
コード例 #3
0
        public async Task <IActionResult> DeletePost(int?id)
        {
            Enjoy enjoy = await _context.Enjoys.FindAsync(id);

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

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

            _context.Enjoys.Remove(enjoy);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #4
0
        public async Task <IActionResult> Update(int?id, Enjoy enjoy)
        {
            Enjoy enjoyDb = await _context.Enjoys.FindAsync(id);

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

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

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

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

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

                enjoyDb.Image = filename;
            }

            enjoyDb.Title = enjoy.Title;

            await _context.SaveChangesAsync();

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