Following the Microsoft docs for Razor Pages. The following code behind code is the standard for creating a customer record:
using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using RazorPagesContacts.Data; namespace RazorPagesContacts.Pages { public class CreateModel : PageModel { private readonly AppDbContext _db; public CreateModel(AppDbContext db) { _db = db; } [BindProperty] public Customer Customer { get; set; } public async Task<IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return Page(); } _db.Customers.Add(Customer); await _db.SaveChangesAsync(); return RedirectToPage("/Index"); } } }
I'm trying to figure out how to make this code "generic" - IOW's somehow be able to handle a "customer model" or a "person model" or whatever. Just don't know where to start. Can anyone get me started?