Files
everything2/EveryThing/Pages/AdministrationCompanies/Edit.cshtml.cs
David Štaleker 03b92525d7 Prvi commit
2023-05-12 09:00:07 +02:00

88 lines
2.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 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<IdentityApplicationUser> _userManager;
public EditModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
[BindProperty]
public CodeTableCompany Company { get; set; }
public new IList<IdentityApplicationUser> User { get; set; }
public async Task<IActionResult> 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<IActionResult> 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);
}
}
}