In this article I will explain with an example, how to export (Grid) GridView to HTML File (HTML String) in ASP.Net MVC Razor.
First, the Grid (GridView) will be populated from database using Entity Framework and then the generated HTML Table of Grid (GridView) will be sent as HTML string to the Controller which will be exported to HTML (.htm) file in ASP.Net MVC Razor.
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
ASP.Net MVC: Export Grid (GridView) to HTML File (HTML String)
You will need to import the following namespace.
using System.Text;
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside this Action method, the Top 10 records from the Customers Table are fetched using Entity Framework and returned to the View.
Action method for handling the HTML File Export and Download operation
This Action method is executed when the Export Submit button is clicked.
Note: The following Action method performs File Download and hence the return type is set to FileResult. For more details on FileResult, please refer my article FileResult return type in ASP.Net MVC.
The HTML of the Grid (GridView) sent from the View is extracted from the GridHtml parameter.
Note: By default posting of HTML content via input fields is disabled. For more details on enabling it, please refer my article What is ValidateInput(false) attribute, its Uses and Examples in ASP.Net MVC.
Finally, the HTML is read and converted to Byte Array and exported and downloaded as HTML (.htm) file using the File function.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        NorthwindEntities entities = new NorthwindEntities();
        return View(from customer in entities.Customers.Take(10)
                    select customer);
    public FileResult Export(string GridHtml)
        return File(Encoding.ASCII.GetBytes(GridHtml), "text/html", "Grid.htm");
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that the Model will be available as a Collection.
Displaying the records
For displaying the records in Grid (GridView), an HTML Table is used.
A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
Exporting the Grid (GridView) to HTML (.htm) file
Below the HTML Table, there is an HTML Submit button enclosed inside a Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Export.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Also, there is an HTML Hidden Field element which will be used to send the HTML content of the Grid (GridView) to the Controller’s Action method.
When the Submit button is clicked, first the HTML of the Grid (GridView) is extracted and set into the Hidden Field element and finally, the Form is submitted.
@model IEnumerable<Customer>
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <div id="Grid">
        <table cellpadding="5" cellspacing="0" style="border: 1px solid #ccc;">
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">CustomerID</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">ContactName</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">City</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">Country</th>
            @foreach (Customer customer in Model)
                    <td style="border: 1px solid #ccc">@customer.CustomerID</td>
                    <td style="border: 1px solid #ccc">@customer.ContactName</td>
                    <td style="border: 1px solid #ccc">@customer.City</td>
                    <td style="border: 1px solid #ccc">@customer.Country</td>
    @using (Html.BeginForm("Export", "Home", FormMethod.Post))
        <input type="hidden" name="GridHtml" />
        <input type="submit" id="btnSubmit" value="Export" />
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(function () {
            $("#btnSubmit").click(function () {
ASP.Net MVC: Export Grid (GridView) to HTML File (HTML String)
GridView exported as HTML (.htm) file
ASP.Net MVC: Export Grid (GridView) to HTML File (HTML String)