From 00c3b32e2db200a2f42396ac7f8381704ec97268 Mon Sep 17 00:00:00 2001 From: Paweł Bernaciak Date: Fri, 10 Feb 2023 16:19:46 +0100 Subject: Initial Commit --- CoreWiki/Pages/Create.cshtml.cs | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 CoreWiki/Pages/Create.cshtml.cs (limited to 'CoreWiki/Pages/Create.cshtml.cs') diff --git a/CoreWiki/Pages/Create.cshtml.cs b/CoreWiki/Pages/Create.cshtml.cs new file mode 100644 index 0000000..ccde2ff --- /dev/null +++ b/CoreWiki/Pages/Create.cshtml.cs @@ -0,0 +1,44 @@ +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 CoreWiki.Models; + +namespace CoreWiki.Pages +{ + public class CreateModel : PageModel + { + private readonly CoreWiki.Models.ApplicationDbContext _context; + + public CreateModel(CoreWiki.Models.ApplicationDbContext context) + { + _context = context; + } + + public IActionResult OnGet() + { + return Page(); + } + + [BindProperty] + public Article Article { get; set; } = default!; + + + // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD + public async Task OnPostAsync() + { + if (!ModelState.IsValid || _context.Articles == null || Article == null) + { + return Page(); + } + + _context.Articles.Add(Article); + await _context.SaveChangesAsync(); + + return Redirect($"/{Article.Topic}"); + } + } +} -- cgit v1.2.3