コード例 #1
0
        public async Task <IActionResult> Edit(int?id, ColdMenu coldMenu)
        {
            ColdMenu coldMenuDb = await _context.ColdMenus.FindAsync(id);

            if (!ModelState.IsValid)
            {
                return(View(coldMenu));
            }
            if (coldMenu.Photo != null)
            {
                if (coldMenu.Photo.ContentType.Contains("image/"))
                {
                    string path = _env.WebRootPath + @"\image\" + coldMenuDb.Image;
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                    coldMenuDb.Image = await coldMenu.Photo.SaveFileAsync(_env.WebRootPath);
                }
                else
                {
                    ModelState.AddModelError("Photo", "Image is not valid!");
                    return(View(coldMenu));
                }
            }
            coldMenuDb.Name        = coldMenu.Name;
            coldMenuDb.Price       = coldMenu.Price;
            coldMenuDb.Ingredients = coldMenu.Ingredients;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        public async Task <IActionResult> DeletePost(int?id)
        {
            ColdMenu coldMenu = await _context.ColdMenus.FindAsync(id);

            if (id == null)
            {
                return(NotFound());
            }
            if (coldMenu == null)
            {
                return(NotFound());
            }
            string path = _env.WebRootPath + @"\image\" + coldMenu.Image;

            if (_context.ColdMenus.ToList().Count > 1)
            {
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }

                _context.ColdMenus.Remove(coldMenu);
                await _context.SaveChangesAsync();
            }
            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public async Task <IActionResult> Delete(int?id)
        {
            ColdMenu coldMenu = await _context.ColdMenus.FindAsync(id);

            if (id == null)
            {
                return(NotFound());
            }
            if (coldMenu == null)
            {
                return(NotFound());
            }
            return(View(coldMenu));
        }
コード例 #4
0
        public async Task <IActionResult> Create(ColdMenu coldMenu)
        {
            if (!ModelState.IsValid)
            {
                return(View(coldMenu));
            }
            if (coldMenu.Photo == null)
            {
                ModelState.AddModelError("Photo", "Please select any image!");
                return(View(coldMenu));
            }
            if (!coldMenu.Photo.ContentType.Contains("image/"))
            {
                ModelState.AddModelError("Photo", "Image is not valid!");
                return(View(coldMenu));
            }
            coldMenu.Image = await coldMenu.Photo.SaveFileAsync(_env.WebRootPath);

            await _context.ColdMenus.AddAsync(coldMenu);

            await _context.SaveChangesAsync();

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