Files
everything/EveryThing/Pages/CodeTableEmployees/Create.cshtml.cs
David Štaleker db0cc8d3de prvi
2025-07-18 05:33:16 +02:00

86 lines
2.6 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 Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using EveryThing.Models.CodeTable;
namespace EveryThing.Pages.CodeTableEmployees
{
[Authorize]
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityApplicationUser> _userManager;
[BindProperty]
public CodeTableEmployee Employee { get; set; }
public CreateModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
public async Task<IActionResult> OnGetAsync()
{
var user = await _userManager.GetUserAsync(User);
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
//if (ModelState.IsValid)
//{
// _context.Add(employee);
// await _context.SaveChangesAsync();
// //Izbrana profilna slika
// if (employee.ProfileImage != null)
// {
// var file = employee.ProfileImage;
// var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
// //Pot datoteke
// var fileName = employee.IdEmployee + Path.GetExtension(parsedContentDisposition.FileName.ToString());
// var filePath = Path.Combine(_hostingEnvironment.WebRootPath, "uploads", "profile-images", fileName);
// //Nalaganje
// using (var stream = System.IO.File.OpenWrite(filePath))
// {
// await file.CopyToAsync(stream);
// }
// }
// return RedirectToAction(nameof(Index));
//}
if (!ModelState.IsValid)
{
return Page();
}
var user = _userManager.GetUserAsync(User).Result;
Employee.IdCompanyFk = user.IdCompanyFk;
Employee.Active = true;
_context.CodeTableEmployees.Add(Employee);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}