Introduction
This article is a continuation of the article, “Creating Page Model And Performing CRUD Operations In ASP.NET Core 3 Razor Pages”. In this article, we will be creating a razor page to implement the create functionality of crud operations.
Creating a Razor page
First, we have to create a new razor page. In order to create a new razor page, right-click on the BookList folder. Click on the Add option. Then click on the razor page option to open the “Add New Scaffolded Item” window. Select the Razor Page option, then click on Add button to add a new razor page.
Inside the 'Add Razor Page' window, write the name of the razor page. Select options, then Generate Page Model class and use a layout page. Click on the 'Add' button. Then, the create razor page will be created.
When the page loads, we want to display text boxes so that the user can enter a book name and author name.
So we need Application Db Context in order to post data to the database when the user clicks the create button. Declare an object of type Application Db Context. Now, create a constructor and pass Application Db Context object as a parameter to the constructor.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using BookList.Model;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- namespace BookList
- {
- public class CreateModel : PageModel
- {
- private readonly ApplicationDbContext _db;
- public CreateModel(ApplicationDbContext db)
- {
- _db = db;
- }
- }
- }
Inside the get handler method, we need to display text boxes
to write the author's name and book name.
The model which we will be using is going to be the Book model. Inside OnGet handler method, we do not have to write that we are passing this empty book object since it does that automatically.
Therefore, inside the create view, we will be able to access the book object and display labels as well as text boxes. Inside the Book model let’s add a new property called ISBN along with getter and setter.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Threading.Tasks;
- namespace BookList.Model
- {
- public class Book
- {
- [Key]
- public int Id { get; set; }
- [Required]
- public string Name { get; set; }
- public string Author { get; set; }
- public string ISBN { get; set; }
- }
- }
Since we have modified the Book model, we have to add a new migration.
To add migration, go to Tools, Click on Nuget Package Manager and then click on Package Manager Console. Inside Package Manager Console, type add-migration AddISBNToBookModel.

To make changes inside the SQL server database, type command update-database.
This is how a new column will be added inside the table in order to changes to the model that we already added. Next, we need to change the Ind.cshml file accordingly.

Craig SunderlandPosted Apr 8, 2020, 10:46 AM
Very nice tutorial.It looks remarkably like a video tutorial from Bhrugen Patel on Udemy.
Inzi BhaiPosted Mar 27, 2020, 1:21 PM
Can You write CRUD article for Razor pages using Dapper, without using WEB API approach... Just for user who are from webform background and have no experience in MVC