JPHD-2021-backend/Pages/Submissions/Edit.cshtml.cs

79 lines
2.0 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.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using _2021_backend.Data;
using _2021_backend.Models;
namespace _2021_backend.Pages.Submissions
{
public class EditModel : PageModel
{
public readonly _2021_backend.Data.BackendContext Context;
public EditModel(_2021_backend.Data.BackendContext context)
{
Context = context;
}
[BindProperty]
public Submission Submission { get; set; }
public async Task<IActionResult> OnGetAsync(string? strid)
{
Guid id = Guid.Parse(strid);
if (id == null)
{
return NotFound();
}
Submission = await Context.Submissions.FirstOrDefaultAsync(m => m.Guid == id);
if (Submission == null)
{
return NotFound();
}
return Page();
}
// 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();
}
Context.Attach(Submission).State = EntityState.Modified;
try
{
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!SubmissionExists(Submission.Guid))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Index");
}
private bool SubmissionExists(Guid id)
{
return Context.Submissions.Any(e => e.Guid == id);
}
}
}