mirror of
https://github.com/dotnet/intro-to-dotnet-web-dev.git
synced 2025-12-06 05:32:03 +01:00
78 lines
2 KiB
C#
78 lines
2 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 RazorPagesPizza.Data;
|
|
using RazorPagesPizza.Models;
|
|
|
|
namespace RazorPagesPizza.Pages
|
|
{
|
|
public class EditModel : PageModel
|
|
{
|
|
private readonly RazorPagesPizza.Data.RazorPagesPizzaContext _context;
|
|
|
|
public EditModel(RazorPagesPizza.Data.RazorPagesPizzaContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[BindProperty]
|
|
public Pizza Pizza { get; set; } = default!;
|
|
|
|
public async Task<IActionResult> OnGetAsync(int? id)
|
|
{
|
|
if (id == null || _context.Pizza == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var pizza = await _context.Pizza.FirstOrDefaultAsync(m => m.Id == id);
|
|
if (pizza == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
Pizza = pizza;
|
|
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(Pizza).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!PizzaExists(Pizza.Id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return RedirectToPage("./Index");
|
|
}
|
|
|
|
private bool PizzaExists(int id)
|
|
{
|
|
return (_context.Pizza?.Any(e => e.Id == id)).GetValueOrDefault();
|
|
}
|
|
}
|
|
}
|