Files
Bewerbungs-Portfolio/RazorPagesMovie/Pages/MoviePages/Delete.cshtml.cs
T
Francesco Lorenzo D'Amico 0e5d5c2a17 Initial commit
2026-07-09 16:40:11 +02:00

59 lines
1.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using RazorPagesMovie.Data;
using RazorPagesMovie.Models;
namespace RazorPagesMovie.Pages.MoviePages;
public class DeleteModel : PageModel
{
private readonly RazorPagesMovieContext _context;
public DeleteModel(RazorPagesMovieContext context)
{
_context = context;
}
[BindProperty]
public Movie Movie { get; set; } = default!;
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id is null)
{
return NotFound();
}
var movie = await _context.Movie.FirstOrDefaultAsync(m => m.Id == id);
if (movie is null)
{
return NotFound();
}
else
{
Movie = movie;
}
return Page();
}
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id is null)
{
return NotFound();
}
var movie = await _context.Movie.FindAsync(id);
if (movie != null)
{
Movie = movie;
_context.Movie.Remove(Movie);
await _context.SaveChangesAsync();
}
return RedirectToPage("./Index");
}
}