Exemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Loot = await _context.Loot.FindAsync(id);

            if (Loot != null)
            {
                _context.Loot.Remove(Loot);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 2
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            string path          = "";
            string previousImage = String.IsNullOrEmpty(Loot.LootImageName)
                ? ""
                : Loot.LootImageName;

            if (image != null)
            {
                Loot.LootImageName = Loot.LootId + Path.GetExtension(image.FileName);
                path = Path.Combine(_environment.WebRootPath, "images", Loot.LootImageName);
            }
            _context.Attach(Loot).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();

                if (image != null)
                {
                    if (!String.IsNullOrEmpty(previousImage))
                    {
                        var fileInfo = _environment.WebRootFileProvider
                                       .GetFileInfo("/Images/" + previousImage);
                        if (fileInfo.Exists)
                        {
                            var oldPath = Path.Combine(_environment.WebRootPath, "images", previousImage);
                            System.IO.File.Delete(oldPath);
                        }
                    }
                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await image.CopyToAsync(stream);
                    };
                }
            }
            finally
            {
                _context.Attach(Loot).State = EntityState.Modified;
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LootExists(Loot.LootId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }