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; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using EveryThing.Models.CodeTable; namespace EveryThing.Pages.AdministrationCompanies { [Authorize(Roles = "Administrator")] public class EditModel : PageModel { private readonly ApplicationDbContext _context; private readonly UserManager _userManager; public EditModel(ApplicationDbContext context, UserManager userManager) { _context = context; _userManager = userManager; } [BindProperty] public CodeTableCompany Company { get; set; } public new IList User { get; set; } public async Task OnGetAsync(int? id) { if (id == null) { return NotFound(); } Company = await _context.CodeTableCompanies.Include(c => c.Country).FirstOrDefaultAsync(m => m.IdCompany == id); User = await _userManager.Users.Include(x => x.Company).Where(x => x.IdCompanyFk == id).ToListAsync(); ViewData["IdCompany"] = id; if (Company == null) { return NotFound(); } ViewData["IdCountryFk"] = new SelectList(_context.CodeTableCountries, "IdCountry", "TranslationSlovenian"); return Page(); } public async Task OnPostAsync() { if (!ModelState.IsValid) { return Page(); } _context.Attach(Company).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CompanyExists(Company.IdCompany)) { return NotFound(); } else { throw; } } return RedirectToPage("./Index"); } private bool CompanyExists(int id) { return _context.CodeTableCompanies.Any(e => e.IdCompany == id); } } }