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

80 lines
2.0 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 EveryThing.Models.CodeTable;
namespace EveryThing.Pages.CodeTableEmployees
{
public class EditModel : PageModel
{
private readonly ApplicationDbContext _context;
public EditModel(ApplicationDbContext context)
{
_context = context;
}
[BindProperty]
public CodeTableEmployee Employee { get; set; }
public IList<Document> Document { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
Employee = await _context.CodeTableEmployees.FirstOrDefaultAsync(m => m.IdEmployee == id);
Document = await _context.Documents.Include(d => d.DocumentType).ToListAsync();
if (Employee == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(Employee).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(Employee.IdEmployee))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Index");
}
private bool EmployeeExists(int id)
{
return _context.CodeTableEmployees.Any(e => e.IdEmployee == id);
}
}
}