Files
everything2/EveryThing/Pages/Documents/Delete.cshtml.cs
David Štaleker 03b92525d7 Prvi commit
2023-05-12 09:00:07 +02:00

61 lines
1.4 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 EveryThing.Data;
using EveryThing.Models;
namespace EveryThing.Pages.Documents
{
public class DeleteModel : PageModel
{
private readonly ApplicationDbContext _context;
public DeleteModel(ApplicationDbContext context)
{
_context = context;
}
[BindProperty]
public Document Document { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
Document = await _context.Documents
.Include(d => d.DocumentType).FirstOrDefaultAsync(m => m.IdDocument == id);
if (Document == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
Document = await _context.Documents.FindAsync(id);
if (Document != null)
{
_context.Documents.Remove(Document);
await _context.SaveChangesAsync();
}
return RedirectToPage("./Index");
}
}
}