In this article I will explain with an example, how to use DataSet (DataTable) as Model in ASP.Net Core (.Net Core 8) Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 8) Razor Pages, please refer my article ASP.Net Core 8 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

Database

I have made use of the following table Customers with the schema as follow.
ASP.Net Core Razor Pages: Using DataSet (DataTable) as Model in Razor Page
 
I have already inserted few records in the table.
ASP.Net Core Razor Pages: Using DataSet (DataTable) as Model in Razor Page
 
Note: You can download the database table SQL by clicking the download link below.
            Download SQL file
 
 

Namespaces

You will need to import the following namespaces.
using System.Data;
using System.Data.SqlClient;
 
 

Razor PageModel (Code-Behind)

Inside the IndexModel, a public property of DataSet is created.
The PageModel consists of following Handler method.

Handler Method for handling GET operation

Inside this Handler method, first the connection string is read from the ConnectionStrings section of the AppSettings.json file.
Note: For more details on reading connection string from AppSettings.json file, please refer my article .Net Core 8: Read Connection String from AppSettings.json file.
 
Then, a connection to the database is established using the SqlConnection class.
Finally, the SqlDataAdapter object is initialized with the SqlCommand and using the Fill function, the DataSet is populated with the records from database.
public class IndexModel : PageModel
{
    public IConfiguration Configuration { get; set; }
 
    public IndexModel(IConfiguration configuration)
    {
        this.Configuration = configuration;
    }
 
    public DataSet Customers { get; set; }
 
    public void OnGet()
    {
        string sql "SELECT CustomerId, name, Country FROM Customers";
        string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    this.Customers = new DataSet();
                    sda.Fill(this.Customers);
                }
            }
        }
    }
}
 
 

Razor Page (HTML)

HTML Markup

The HTML of Razor Page consists of an HTML Table for displaying the records.
Then, a FOR EACH loop will be executed over the Model property which will generate the HTML Table rows with the Customer records.
@page
@using System.Data;
@modelDataSet_Razor_Core.Pages.IndexModel
 
@{
     Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>CustomerId</th>
            <th>Name</th>
            <th>Country</th>
        </tr>
        @foreach (DataRow row in Model.Customers.Tables[0].Rows)
        {
            <tr>
                <td>@row["CustomerId"]</td>
                <td>@row["Name"]</td>
                <td>@row["Country"]</td>
            </tr>
        }
    </table>
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Using DataSet (DataTable) as Model in Razor Page
 
 

Downloads