diff options
| author | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-02-10 16:19:46 +0100 |
|---|---|---|
| committer | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-02-10 16:19:46 +0100 |
| commit | 00c3b32e2db200a2f42396ac7f8381704ec97268 (patch) | |
| tree | 089d68dcbda127d4c95ccca9b0c9a18b59dc8bdc /CoreWiki/Pages/Edit.cshtml.cs | |
Initial Commit
Diffstat (limited to 'CoreWiki/Pages/Edit.cshtml.cs')
| -rw-r--r-- | CoreWiki/Pages/Edit.cshtml.cs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/CoreWiki/Pages/Edit.cshtml.cs b/CoreWiki/Pages/Edit.cshtml.cs new file mode 100644 index 0000000..06d002f --- /dev/null +++ b/CoreWiki/Pages/Edit.cshtml.cs @@ -0,0 +1,76 @@ +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 CoreWiki.Models;
+
+namespace CoreWiki.Pages
+{
+ public class EditModel : PageModel
+ {
+ private readonly CoreWiki.Models.ApplicationDbContext _context;
+
+ public EditModel(CoreWiki.Models.ApplicationDbContext context)
+ {
+ _context = context;
+ }
+
+ [BindProperty]
+ public Article Article { get; set; } = default!;
+
+ public async Task<IActionResult> OnGetAsync(string id)
+ {
+ if (id == null || _context.Articles == null)
+ {
+ return NotFound();
+ }
+
+ var article = await _context.Articles.FirstOrDefaultAsync(m => m.Topic == id);
+ if (article == null)
+ {
+ return NotFound();
+ }
+ Article = article;
+ return Page();
+ }
+
+ // To protect from overposting attacks, 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(Article).State = EntityState.Modified;
+
+ try
+ {
+ await _context.SaveChangesAsync();
+ }
+ catch (DbUpdateConcurrencyException)
+ {
+ if (!ArticleExists(Article.Topic))
+ {
+ return NotFound();
+ }
+ else
+ {
+ throw;
+ }
+ }
+
+ return Redirect($"./{(Article.Topic == "HomePage" ? "" : Article.Topic)}");
+ }
+
+ private bool ArticleExists(string id)
+ {
+ return (_context.Articles?.Any(e => e.Topic == id)).GetValueOrDefault();
+ }
+ }
+}
|
