Introduction
This article is the continuation of the article “Creating a Model and Database in ASP.NET Core Razor Pages Using Entity Framework Core” where we have learned to create a book model and also to create database and tables using entity framework.
Creating Page Model class
In this article, we will discuss how to create a PageModel class to populate all of the books from the database and pass that to a razor page to be displayed. We will also create user interface to display all of the books retrieved from the database so that in later articles we could learn to perform crud operations on book object. For this we need pages to create a new book, edit a new book, delete a book and also view all the books that are available inside the database.
Let’s create a new folder named BookList inside Pages folder.

Now we need to add a new razor page. For that right click on the BookLists folder, click on Add option and then click on Razor page to add a new razor pager in the application.
Select the empty razor page option, so that we can understand the functionality of razor pages.

Inside the Add Razor Page dialog box, write the name of the Razor page as Ind. Below in the Options panel, check the Generate PageModel class and use a layout page option.

Here we need a page model class because we need to populate all of the books from the database and pass that to the page to be displayed. There is no need to check.
Create as partial view option because this will be a complete page. A partial view can be a small subsection like a group of buttons that can be reused in multiple places in an application.
We will use the layout page because that is a master page. Now click on Add button to add the razor page.
Inside the index page model, we will create a functionality to retrieve all of the books from database. For this to happen, we need application Db Context.
We have already added application Db Context in services inside the pipeline as you can see in my previous article. So now it can be used with the help of dependency injection.
- 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;
- using Microsoft.EntityFrameworkCore;
- namespace BookList
- {
- public class IndModel : PageModel
- {
- private readonly ApplicationDbContext _db;
- public IndModel(ApplicationDbContext db)
- {
- _db = db;
- }
- }
- }
This is how application db context can be extracted and injected on a specific page from dependency injection container.
Now we have to return a list or IEnumerable of books. We will assign all of the books from the database to Books object.
- public IEnumerable<Book> Books { get; set; }
With the help of Get handler, we are going to the database, retrieving all of the books and storing that inside IEnumerable object named Books.
- public async Task OnGet()
- {
- Books = await _db.Book.ToListAsync();
- }
Async inside the Get handler method is running multiple tasks at a time until it is awaited. Here we need to await as we need to assign all the books that have been retrieved from the database.
After adding the get handler method for the index page, let’s create user interface to display the books retrieved from the database. Now we can display a table displaying all the books retrieved from database.



Join the conversation! Your thoughts help the community grow.