In this article I will explain with an example, how to display Grand Total in Footer 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

Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 

Model

The Model class consists of following properties.
public class Product
{
    public int OrderID { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
}
 
 

Namespaces

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

Page Model (Code-Behind)

Inside the PageModel class, a property of type Generic List collection is defined.
The PageModel consists of following Handler method.

Handler method for handling GET operation

Inside this Handler method, the records are fetched from the Orders and Product Tables using SqlDataReader and then using WHILE Loop, the records are copied into the Generic List collection of Product class objects.
Note: For more information about using SqlDataReader, please refer my article Using SqlDataReader in ASP.Net Core Razor Pages.
 
public class IndexModel : PageModel
{
    public List<Product> Products { get; set; }
 
    public void OnGet()
    {
        string constr = @"Data Source=.\SQL2019;Initial Catalog=Northwind;uid=sa;pwd=pass@123";
        using (SqlConnection con = new SqlConnection(constr))
        {
            string sql = "SELECT TOP 10 OrderID,";
            sql += "(SELECT ProductName FROM Products WHERE ProductID = details.ProductId) ProductName,";
            sql += "(Quantity * UnitPrice) Price";
            sql += " FROM [Order Details] details";
            sql += " ORDER BY OrderID";
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    this.Products = new List<Product>();
                    while (sdr.Read())
                    {
                        Products.Add(new Product
                        {
                            OrderID = Convert.ToInt32(sdr["OrderID"]),
                            ProductName = sdr["ProductName"].ToString(),
                            Price = Convert.ToDecimal(sdr["Price"])
                        });
                    }
                }
                con.Close();
            }
        }
    }
}
 
 

View

HTML Markup

Inside the Razor Page, an HTML Table is used for displaying Grand Total in Footer.
For displaying the records, an HTML Table is used. A loop will be executed over the rows of the DataTable which will generate the HTML Table rows with the records.
Note: The Price column is formatted to display only two decimal places using the ToString(“N2”) function.
 
The last row of the HTML Table is the Footer Row, where the Grand Total i.e. the Sum of the Price of all the Products is calculated using Lambda Expression and displayed.
@page
@model Calculate_Total_Table_Core_Razor.IndexModel
@using Calculate_Total_Table_Core_Razor.Models;
@{
    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>Order ID</th>
            <th>Product Name</th>
            <th>Price</th>
        </tr>
        @foreach (Product product in Model.Products)
        {
            <tr>
                <td>@product.OrderID</td>
                <td>@product.ProductName</td>
                <td>@product.Price.ToString("N2")</td>
            </tr>
        }
        <tr>
            <td colspan="2" align="right">Total</td>
            <td>@Model.Products.Select(p => p.Price).Sum().ToString("N2")</td>
        </tr>
    </table>
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Display Grand Total in Footer
 
 

Downloads