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

85 lines
2.4 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.Vehicle;
namespace EveryThing.Pages.Notes
{
public class EditModel : PageModel
{
private readonly ApplicationDbContext _context;
public EditModel(ApplicationDbContext context)
{
_context = context;
}
[BindProperty]
public GeneralNote Note { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
Note = await _context.GeneralNotes
.Include(n => n.Company)
.Include(n => n.Employee)
.Include(n => n.Vehicle).FirstOrDefaultAsync(m => m.IdNote == id);
if (Note == null)
{
return NotFound();
}
ViewData["IdCompanyFk"] = new SelectList(_context.CodeTableCompanies, "IdCompany", "Ceo");
ViewData["IdEmployeeFk"] = new SelectList(_context.CodeTableEmployees, "IdEmployee", "BankAccount");
ViewData["IdVehicleFk"] = new SelectList(_context.Set<Vehicle>(), "IdVehicle", "RegistrationNumber");
return Page();
}
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(Note).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!NoteExists(Note.IdNote))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Index");
}
private bool NoteExists(int id)
{
return _context.GeneralNotes.Any(e => e.IdNote == id);
}
}
}