mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
43 lines
929 B
C#
43 lines
929 B
C#
|
|
using Microsoft.AspNetCore.Mvc.RazorPages; // PageModel
|
||
|
|
using Packt.Shared; // NorthwindContext
|
||
|
|
using Microsoft.AspNetCore.Mvc; // [BindProperty], IActionResult
|
||
|
|
|
||
|
|
namespace Northwind.Web.Pages;
|
||
|
|
|
||
|
|
public class SuppliersModel : PageModel
|
||
|
|
{
|
||
|
|
private NorthwindContext db;
|
||
|
|
|
||
|
|
public SuppliersModel(NorthwindContext injectedContext)
|
||
|
|
{
|
||
|
|
db = injectedContext;
|
||
|
|
}
|
||
|
|
|
||
|
|
public IEnumerable<Supplier>? Suppliers { get; set; }
|
||
|
|
|
||
|
|
public void OnGet()
|
||
|
|
{
|
||
|
|
ViewData["Title"] = "Northwind B2B - Suppliers";
|
||
|
|
|
||
|
|
Suppliers = db.Suppliers
|
||
|
|
.OrderBy(c => c.Country).ThenBy(c => c.CompanyName);
|
||
|
|
}
|
||
|
|
|
||
|
|
[BindProperty]
|
||
|
|
public Supplier? Supplier { get; set; }
|
||
|
|
|
||
|
|
public IActionResult OnPost()
|
||
|
|
{
|
||
|
|
if ((Supplier is not null) && ModelState.IsValid)
|
||
|
|
{
|
||
|
|
db.Suppliers.Add(Supplier);
|
||
|
|
db.SaveChanges();
|
||
|
|
return RedirectToPage("/suppliers");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return Page(); // return to original page
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|