Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Razor Pages - Generic Edit / Create

$
0
0

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?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>