prvi
This commit is contained in:
79
EveryThing/Pages/Documents/Edit.cshtml.cs
Normal file
79
EveryThing/Pages/Documents/Edit.cshtml.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
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;
|
||||
|
||||
namespace EveryThing.Pages.Documents
|
||||
{
|
||||
public class EditModel : PageModel
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public EditModel(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public Document Document { get; set; }
|
||||
|
||||
public async Task<IActionResult> OnGetAsync(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Document = await _context.Documents
|
||||
.Include(d => d.DocumentType).FirstOrDefaultAsync(m => m.IdDocument == id);
|
||||
|
||||
if (Document == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
ViewData["IdDocumentTypeFk"] = new SelectList(_context.DocumentTypes, "IdDocumentType", "Title");
|
||||
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(Document).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!DocumentExists(Document.IdDocument))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return RedirectToPage("./Index");
|
||||
}
|
||||
|
||||
private bool DocumentExists(int id)
|
||||
{
|
||||
return _context.Documents.Any(e => e.IdDocument == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user