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

61 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using EveryThing.Data;
using EveryThing.Models;
using EveryThing.Models.CodeTable;
using Microsoft.AspNetCore.Authorization;
namespace EveryThing.Pages.CodeTablePartners
{
[Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser,TransportThingUser")]
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityApplicationUser> _userManager;
private readonly SignInManager<IdentityApplicationUser> _loginManager;
private readonly RoleManager<IdentityApplicationRole> _roleManager;
public CreateModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
{
_context = context;
_userManager = userManager;
_loginManager = loginManager;
_roleManager = roleManager;
}
public IActionResult OnGet()
{
ViewData["IdCountryFk"] = new SelectList(_context.CodeTableCountries, "IdCountry", "TranslationSlovenian");
return Page();
}
[BindProperty]
public CodeTablePartner Partner { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var user = _userManager.GetUserAsync(User).Result;
Partner.IdCompanyFk = user.IdCompanyFk;
Partner.Active = true;
_context.CodeTablePartners.Add(Partner);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}