Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.RazorPages;
  7. using Microsoft.AspNetCore.Mvc.Rendering;
  8. using Microsoft.EntityFrameworkCore;
  9. using RazorPagesMovies.Models;
  10. using RazorPagesMovies.Movies;
  11.  
  12. namespace RazorPagesMovies.Pages.Movies
  13. {
  14.     public class EditModel : PageModel
  15.     {
  16.         private readonly RazorPagesMovies.Models.MoviesContext _context;
  17.  
  18.         public EditModel(RazorPagesMovies.Models.MoviesContext context)
  19.         {
  20.             _context = context;
  21.         }
  22.  
  23.         [BindProperty]
  24.         public Movie Movie { get; set; }
  25.  
  26.         public async Task<IActionResult> OnGetAsync(int? id)
  27.         {
  28.             if (id == null)
  29.             {
  30.                 return NotFound();
  31.             }
  32.  
  33.             Movie = await _context.Movie.FirstOrDefaultAsync(m => m.ID == id);
  34.  
  35.             if (Movie == null)
  36.             {
  37.                 return NotFound();
  38.             }
  39.             return Page();
  40.         }
  41.  
  42.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  43.         // more details see https://aka.ms/RazorPagesCRUD.
  44.         public async Task<IActionResult> OnPostAsync()
  45.         {
  46.             if (!ModelState.IsValid)
  47.             {
  48.                 return Page();
  49.             }
  50.  
  51.             _context.Attach(Movie).State = EntityState.Modified;
  52.  
  53.             try
  54.             {
  55.                 await _context.SaveChangesAsync();
  56.             }
  57.             catch (DbUpdateConcurrencyException)
  58.             {
  59.                 if (!MovieExists(Movie.ID))
  60.                 {
  61.                     return NotFound();
  62.                 }
  63.                 else
  64.                 {
  65.                     throw;
  66.                 }
  67.             }
  68.  
  69.             return RedirectToPage("./Index");
  70.         }
  71.  
  72.         private bool MovieExists(int id)
  73.         {
  74.             return _context.Movie.Any(e => e.ID == id);
  75.         }
  76.     }
  77. }
  78.