67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Snootalogue.Data;
|
|
using Snootalogue.Models;
|
|
using Snootalogue.ViewModels;
|
|
|
|
namespace Snootalogue.Pages.Documents {
|
|
public class EditModel : PageModel {
|
|
private readonly SnootalogueContext _context;
|
|
[BindProperty]
|
|
public EditDocument ed { get; set; }
|
|
|
|
public EditModel(SnootalogueContext context) {
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetAsync(int? id) {
|
|
if (id == null) {
|
|
return NotFound();
|
|
}
|
|
|
|
var Document = await _context.Document.FirstOrDefaultAsync(d => d.ID == id);
|
|
|
|
if (Document == null) {
|
|
return NotFound();
|
|
}
|
|
|
|
ed = new EditDocument {
|
|
ID = Document.ID,
|
|
Title = Document.Title,
|
|
Authors = Document.Authors,
|
|
Category = Document.Category,
|
|
Tags = Document.Tags,
|
|
Read = Document.Read
|
|
};
|
|
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync() {
|
|
if (!ModelState.IsValid) {
|
|
return Page();
|
|
}
|
|
|
|
Document d = _context.Document.First(d => d.ID == ed.ID);
|
|
d.Title = ed.Title;
|
|
d.Category = ed.Category;
|
|
d.Notes = ed.Notes;
|
|
d.Read = ed.Read;
|
|
|
|
_context.Attach(d).State = EntityState.Modified;
|
|
|
|
try {
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException e) {
|
|
// TODO: handle it like https://github.com/dotnet/AspNetCore.Docs/blob/master/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Pages/Movies/Edit.cshtml.cs#L56
|
|
throw e;
|
|
}
|
|
|
|
return RedirectToPage("/Index");
|
|
}
|
|
}
|
|
}
|