In this article I will explain with an example, how to export WebGrid to CSV file with formatting in ASP.Net MVC Razor.
First the WebGrid will be populated from database using Entity Framework and when the Export Button is clicked, a Comma Separated (Delimited) string will be generated from WebGrid data and ultimately it will be exported to CSV 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.
Export WebGrid to CSV file in ASP.Net MVC
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, the Top 10 Customer records are fetched and returned to the View.
Action method for handling the CSV 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.
The WebGrid data is converted into a Comma Separated (Delimited) string and then the Comma Separated (Delimited) string is converted into Byte Array using the GetBytes functions of the ASCII class.
Finally, it is exported and downloaded as CSV 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()
        NorthwindEntities entities = new NorthwindEntities();
        List<object> customers = (from customer in entities.Customers.ToList().Take(10)
                                    select new[] { customer.CustomerID.ToString(),
        //Insert the Column Names.
        customers.Insert(0, new string[4] { "Customer ID", "Customer Name", "City", "Country" });
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < customers.Count; i++)
            string[] customer = (string[])customers[i];
            for (int j = 0; j < customer.Length; j++)
                //Append data with separator.
                sb.Append(customer[j] + ',');
            //Append new line character.
        return File(Encoding.UTF8.GetBytes(sb.ToString()), "text/csv", "Grid.csv");
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.
For displaying the records, the WebGrid is rendered using GetHtml function which renders the WebGrid using Model.
Note: For more details on using WebGrid, please refer WebGrid Step By Step Tutorial with example in ASP.Net MVC.
The WebGrid will render as an HTML Grid (Table) consisting of the records from the Customers Table.
Finally there’s an HTML Submit button enclosed inside a Form with the Action method specified as Export.
When this Button will be clicked, the Form is submitted and the Export Action method is called.
@model IEnumerable<Customer>
    Layout = null;
    WebGrid webGrid = new WebGrid(source: Model, canSort: false, canPage: false);
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    <div id="Grid">
        htmlAttributes: new { @id = "WebGrid" },
        columns: webGrid.Columns(
                 webGrid.Column("CustomerID", "Customer Id"),
                 webGrid.Column("ContactName", "Customer Name"),
                 webGrid.Column("City", "City"),
                 webGrid.Column("Country", "Country")))
    @using (Html.BeginForm("Export", "Home", FormMethod.Post))
        <input type="submit" id="btnSubmit" value="Export"/>
Export WebGrid to CSV file in ASP.Net MVC
Exported CSV File
Export WebGrid to CSV file in ASP.Net MVC