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 EveryThing.Data; using EveryThing.Models.Vehicle; namespace EveryThing.Pages.FleetFueling { public class EditModel : PageModel { private readonly ApplicationDbContext _context; public EditModel(ApplicationDbContext context) { _context = context; } [BindProperty] public Models.Vehicle.VehicleFueling VehicleFueling { get; set; } public async Task OnGetAsync(int? id) { if (id == null) { return NotFound(); } VehicleFueling = await _context.VehicleFuelings .Include(v => v.Country) .Include(v => v.Employee) .Include(v => v.Vehicle) .Include(v => v.VehicleFuelType) .Include(v => v.VehicleFuelingCard).FirstOrDefaultAsync(m => m.IdVehicleFueling == id); if (VehicleFueling == null) { return NotFound(); } ViewData["IdCountryFk"] = new SelectList(_context.CodeTableCountries, "IdCountry", "Title"); ViewData["IdEmployeeFk"] = new SelectList(_context.CodeTableEmployees, "IdEmployee", "BankAccount"); ViewData["IdVehicleFk"] = new SelectList(_context.Vehicles, "IdVehicle", "RegistrationNumber"); ViewData["IdVehicleFuelTypeFk"] = new SelectList(_context.VehicleFuelTypes, "IdVehicleFuelType", "Title"); ViewData["IdVehicleFuelingCardFk"] = new SelectList(_context.VehicleFuelingCards, "IdFuelingCard", "CardNumber"); return Page(); } // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task OnPostAsync() { if (!ModelState.IsValid) { return Page(); } _context.Attach(VehicleFueling).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!VehicleFuelingExists(VehicleFueling.IdVehicleFueling)) { return NotFound(); } else { throw; } } return RedirectToPage("./Index"); } private bool VehicleFuelingExists(int id) { return _context.VehicleFuelings.Any(e => e.IdVehicleFueling == id); } } }