Manipulating data

Let's add the functionality to insert a new supplier.

Open Suppliers.cshtml.cs, and import the following namespace:

using Microsoft.AspNetCore.Mvc;

In the SuppliersModel class, add a property to store a supplier, and a method named OnPost that adds the supplier if its model is valid, as shown in the following code:

[BindProperty]public Supplier Supplier { get; set; }public IActionResult OnPost(){  if (ModelState.IsValid)  {     db.Suppliers.Add(Supplier);     db.SaveChanges();     return RedirectToPage("/suppliers");  }  return Page();}

Open Suppliers.cshtml, and add tag helpers, as shown in the following markup:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

At the bottom of the .cshtml file, add a form to insert a new supplier, ...

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.