In this article I will explain with an example, how to create and render Partial View in ASP.Net Core Razor Pages.
The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the Razor Partial tag in ASP.Net Core Razor Pages.
Note: For beginners in ASP.Net Core Razor Pages, please refer my article ASP.Net Core Razor Pages: Hello World Tutorial with Sample Program example.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Note: The Customers Table of the Northwind Database will be used in this project.
 
 
Model
Following class is used as the Model class.
public class Customer
{
    public string CustomerID { get; set; }
    public string ContactName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
} 
 
 
Entity Data Model
Following is the Database Context class which is used to connect to the Database.
Note : For more information on creating Database Context class using Entity Framework, please refer my article ASP.Net Core Razor Pages: Simple Entity Framework Tutorial with example.
 
using Microsoft.EntityFrameworkCore;
using Razor_Entity_Framework.Models;
 
namespace Razor_Entity_Framework
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
    }
}
 
 
Razor PageModel (Code-Behind)
The Database Context is injected into the IndexModel class using Dependency Injection method.
Handler method for handling GET operation
Inside the Handler method that handles the GET calls, the Top 10 records are fetched from the Customers Table of the Northwind Database and returned to the Razor Page.
public class IndexModel : PageModel
{
    private DBCtx Context { get; }
    public IndexModel(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public List<Customer> Customers { get; set; }
 
    public void OnGet()
    {
        this.Customers = (from customer in this.Context.Customers.Take(10)
                          select customer).ToList();
    }
}
 
 
Razor Page (HTML)
The HTML of Razor Page consists of an HTML Table.
A FOR loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
The last column of the HTML Table consists of the Razor Partial tag which renders the Partial View. The name of the View and the object of the Customer model class are passed to the Razor Partial tag’s name and model attributes respectively.
@page
@model Razor_PartialView_EF_Core.Pages.IndexModel
@using Razor_PartialView_EF_Core.Models
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <h4>Customers</h4>
    <hr/>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>CustomerID</th>
            <th>Contact Name</th>
            <th>Address</th>
        </tr>
        @foreach (Customer customer in Model.Customers)
        {
            <tr>
                <td>@customer.CustomerID</td>
                <td>@customer.ContactName</td>
                <td><partial name="Details" model="customer"/></td>
            </tr>
        }
    </table>
</body>
</html>
 
 
Partial View
In order to add Partial View, you will need to Right Click inside the Solution Explorer and click on the Add New Item option in order to add a Partial Razor View.
Inside the Add New Item dialog, the Razor View – Empty options needs to be selected as shown below.
ASP.Net Core Razor Pages - Render Partial View
 
Inside the Partial View, in the very first line the Customer Entity is declared as Model for the Partial View. The details of the Customer are displayed using the Html.DisplayFor helper method.
@model Partial_View_Entity_MVC.Customer
 
@Html.DisplayFor(model => model.Address)
<br/>
@Html.DisplayFor(model => model.City),
@Html.DisplayFor(model => model.PostalCode)
<br/>
@Html.DisplayFor(model => model.Country)
 
 
Screenshot
ASP.Net Core Razor Pages - Render Partial View
 
 
Downloads