auto.bus_razor/TicketOffice/Pages/Management/Tickets/Edit.cshtml.cs

83 lines
2.3 KiB
C#

#nullable disable
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 TicketOffice.Data;
using TicketOffice.Models;
namespace TicketOffice.Pages.Management.Tickets
{
public class EditModel : PageModel
{
private readonly TicketOffice.Data.TicketOfficeContext _context;
public EditModel(TicketOffice.Data.TicketOfficeContext context)
{
_context = context;
}
[BindProperty]
public Ticket Ticket { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
Ticket = await _context.Ticket
.Include(t => t.Route)
.Include(t => t.User).FirstOrDefaultAsync(m => m.Id == id);
if (Ticket == null)
{
return NotFound();
}
ViewData["RouteId"] = new SelectList(_context.Route, "Id", "Number");
ViewData["UserId"] = new SelectList(_context.User, "Id", "Email");
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(Ticket).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TicketExists(Ticket.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Index");
}
private bool TicketExists(int id)
{
return _context.Ticket.Any(e => e.Id == id);
}
}
}