In this article I will explain with an example, how to export WebGrid to CSV (Text) file in ASP.Net Core Razor Pages.
The WebGrid will be populated from database using Entity Framework and then the records from the database will be exported and downloaded as Microsoft CSV (Text) file in ASP.Net Core Razor Pages.
MVC6 Grid for ASP.Net Core
This article makes use of MVC6 Grid library for implementing WebGrid, as it is not available by default in .Net Core.
For more details on how to use MVC6 Grid, please refer the article WebGrid Step By Step Tutorial with example in ASP.Net Core Razor Pages.
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
You need to import the following namespaces.
using System.Linq;
using System.Text;
The Model class consists of the following four properties.
public class Customer
    public string CustomerID { get; set; }
    public string ContactName { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my article ASP.Net Core Razor Pages: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
using Microsoft.EntityFrameworkCore;
namespace Export_WebGrid_CSV_Razor_Core
    public class DBCtx : DbContext
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        public DbSet<Customer> Customers { get; set; }
Razor PageModel (Code-Behind)
The PageModel consists of following two Handler methods.
Handler method for handling GET operation
Inside this Handler method, the Top 10 records are fetched from the Customers table using Entity Framework and assigned to the public property Customers.
Handler method for handling the CSV (Text) file Export and Download operation
This Handler method is executed when the Export Button is clicked.
Note: The following Handler method performs File Download operation. Hence the return type is set to FileResult.
Inside this Handler method, the Top 10 records are fetched from the Customers Table using Entity Framework.
Then, a loop is executed over the records fetched using Entity Framework and a comma separated (delimited) string is generated which is ultimately converted to a Byte Array using the GetBytes method of Encoding class.
Finally, the Byte Array is exported and downloaded as CSV (Text) file using the File function.
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 = this.Context.Customers.Take(10).ToList();
    public FileResult OnPostExport()
        string[] columnNames = new string[] { "CustomerId", "ContactName", "City", "Country" };
        var customers = this.Context.Customers.Take(10).ToList();
        //Build the CSV file data as a Comma separated string.
        string csv = string.Empty;
        foreach (string columnName in columnNames)
            //Add the Header row for CSV file.
            csv += columnName + ',';
        //Add new line.
        csv += "\r\n";
        foreach (var customer in customers)
            //Add the Data rows.
            csv += customer.CustomerID.Replace(",", ";") + ',';
            csv += customer.ContactName.Replace(",", ";") + ',';
            csv += customer.City.Replace(",", ";") + ',';
            csv += customer.Country.Replace(",", ";") + ',';
            //Add new line.
            csv += "\r\n";
        //Download the CSV file.
        byte[] bytes = Encoding.ASCII.GetBytes(csv);
        return File(bytes, "text/csv", "Grid.csv");
Razor Page (HTML)
Inside the Razor Page, ASP.Net TagHelpers, NonFactors.Mvc.Grid namespace and mvc-grid.css file is inherited.
Displaying the Records
For displaying the records, MVC6 Grid is used.
The public property of Generic List object of the Customer Model class is accessed through PageModel inside the Razor Page and passed to the Grid function of the MVC6 Grid HTML Helper class.
Exporting WebGrid to CSV (Text) file
Below the MVC6 WebGrid, there is an HTML Form within which a Submit Button has been placed.
The Export Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostExport but here it will be specified as Export when calling from the Razor HTML Page.
When the Export Button is clicked, the MVC6 WebGrid data will be exported and downloaded as CSV (Text) file.
@model Export_WebGrid_CSV_Razor_Core.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@using NonFactors.Mvc.Grid;
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <link href="~/css/mvc-grid/mvc-grid.css" rel="stylesheet" />
    <hr />
    @(Html.Grid(Model.Customers).Build(columns =>
            columns.Add(model => model.CustomerID).Titled("CustomerID");
            columns.Add(model => model.ContactName).Titled("ContactName");
            columns.Add(model => model.City).Titled("City");
            columns.Add(model => model.Country).Titled("Country");
    <br /><br />
    <form method="post">
        <input type="submit" id="btnSubmit" value="Export" asp-page-handler="Export" />
ASP.Net Core Razor Pages: Export WebGrid to CSV (Text) file
Exported CSV (Text) file
ASP.Net Core Razor Pages: Export WebGridto CSV (Text) file