72 lines
1.6 KiB
C#
72 lines
1.6 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 EditModel : PageModel
|
|
{
|
|
private readonly RazorPagesMovieContext _context;
|
|
|
|
public EditModel(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();
|
|
}
|
|
Movie = movie;
|
|
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(Movie).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!MovieExists(Movie.Id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return RedirectToPage("./Index");
|
|
}
|
|
|
|
private bool MovieExists(int id)
|
|
{
|
|
return _context.Movie.Any(e => e.Id == id);
|
|
}
|
|
}
|