RazorPages/Pages/Climbs/Delete.cshtml.cs

63 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using RazorPages.Data;
using RazorPages.Models;
namespace RazorPages.Pages.Climbs
{
public class DeleteModel : PageModel
{
private readonly RazorPages.Data.RazorPagesClimbContext _context;
public DeleteModel(RazorPages.Data.RazorPagesClimbContext context)
{
_context = context;
}
[BindProperty]
public Climb Climb { get; set; } = default!;
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
var climb = await _context.Climb.FirstOrDefaultAsync(m => m.Id == id);
if (climb is not null)
{
Climb = climb;
return Page();
}
return NotFound();
}
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
var climb = await _context.Climb.FindAsync(id);
if (climb != null)
{
Climb = climb;
_context.Climb.Remove(Climb);
await _context.SaveChangesAsync();
}
return RedirectToPage("./Index");
}
}
}