コード例 #1
0
        public async Task <IActionResult> DeletePost(int id)
        {
            var deleteRoom = await context.Rooms.FindAsync(id);

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

            try
            {
                context.Remove(deleteRoom);
                await context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch (DbUpdateException ex)
            {
                return(RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }));
            }
        }
コード例 #2
0
        public async Task <IActionResult> DeletePost(int id)
        {
            var rooms = await context.Rooms.Include("Images").ToListAsync();

            var deleteRoom = rooms.Find(r => r.Id == id);

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

            try
            {
                context.Remove(deleteRoom);
                await context.SaveChangesAsync();

                return(Redirect("/"));
            }
            catch (DbUpdateException)
            {
                return(RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }));
            }
        }