110 lines
3.5 KiB
C#
110 lines
3.5 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 EveryThing.Data;
|
|
using EveryThing.Models;
|
|
using EveryThing.Models.Vehicle;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using NuGet.Protocol.Plugins;
|
|
using DocumentFormat.OpenXml.Bibliography;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EveryThing.Pages.CodeTableVehicles
|
|
{
|
|
[Authorize(Roles = "Administrator,TransportThingUser")]
|
|
public class CreateEditModel : PageModel
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
|
|
public CreateEditModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
}
|
|
|
|
[BindProperty]
|
|
public Vehicle Vehicle { get; set; }
|
|
|
|
public async Task<IActionResult> OnGetAsync(bool edit, int? id)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
ViewData["Edit"] = edit;
|
|
|
|
if (edit)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
Vehicle = await _context.Vehicles.FirstOrDefaultAsync(x => x.IdCompanyFk == user.IdCompanyFk
|
|
&& x.IdVehicle == id);
|
|
if (Vehicle == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
}
|
|
|
|
ViewData["VehicleType"] = new SelectList(_context.VehicleTypes
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active), "IdVehicleType", "Title");
|
|
|
|
ViewData["VehicleMake"] = new SelectList(_context.VehicleMakes
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active), "IdVehicleMake", "Title");
|
|
|
|
ViewData["FuelType"] = new SelectList(_context.VehicleFuelTypes
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk), "IdVehicleFuelType", "Title");
|
|
|
|
ViewData["Department"] = new SelectList(_context.CodeTableDepartements
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk), "IdDepartement", "Title");
|
|
|
|
|
|
return Page();
|
|
}
|
|
|
|
// To protect from over-posting attacks, see https://aka.ms/RazorPagesCRUD
|
|
public async Task<IActionResult> OnPostAsync(bool edit)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return Page();
|
|
}
|
|
|
|
if (edit)
|
|
{
|
|
_context.Attach(Vehicle).State = EntityState.Modified;
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!VehicleExists(Vehicle.IdVehicle))
|
|
{
|
|
return NotFound();
|
|
}
|
|
throw;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//Add new
|
|
_context.Vehicles.Add(Vehicle);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
return RedirectToPage("./Index");
|
|
}
|
|
|
|
private bool VehicleExists(int id)
|
|
{
|
|
return _context.Vehicles.Any(e => e.IdVehicle == id);
|
|
}
|
|
}
|
|
}
|