In this article I will explain with an example, how to implement Paging and Sorting in ASP.Net MVC Razor.
This article will illustrate how to populate WebGrid with Paging and Sorting enabled using Entity Framework in ASP.Net MVC Razor.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Configuring and connecting Entity Framework to database
Now I will explain the steps to configure and add Entity Framework and also how to connect it with the database.
You will need to add Entity Data Model to your project by right clicking the Solution Explorer and then click on Add and then New Item option of the Context Menu.
Implement Paging and Sorting in ASP.Net MVC
 
From the Add New Item window, select ADO.NET Entity Data Model and set its Name as NorthwindModel and then click Add.
Implement Paging and Sorting in ASP.Net MVC
 
Then the Entity Data Model Wizard will open up where you need to select EF Designer database option.
Implement Paging and Sorting in ASP.Net MVC
 
Now the wizard will ask you to connect and configure the Connection String to the database.
Implement Paging and Sorting in ASP.Net MVC
 
You will need to select the
1.     SQL Server Instance
2.     Database
And then click Test Connection to make sure all settings are correct.
Implement Paging and Sorting in ASP.Net MVC
 
Once the Connection String is generated, click Next button to move to the next step.
Implement Paging and Sorting in ASP.Net MVC
 
Next you will need to choose the Entity Framework version to be used for connection.
Implement Paging and Sorting in ASP.Net MVC
 
Now you will need to choose the Tables you need to connect and work with Entity Framework. Here Customers Table is selected.
Implement Paging and Sorting in ASP.Net MVC
 
The above was the last step and you should now have the Entity Data Model ready with the Customers Table of the Northwind Database.
Implement Paging and Sorting in ASP.Net MVC
 
 
Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
Inside the Index Action method, the Customer records are fetched using Entity Framework and returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        NorthwindEntities entities = new NorthwindEntities();
        return View(entities.Customers.ToList());
    }
}
 
 
View
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.
The WebGrid is initialized with the Model i.e. IEnumerable collection of Customer Entity class objects as source.
The WebGrid is created using the GetHtml method with the following parameters.
HtmlAttributes – It is used to set the HTML attributes to the HTML Table generated by WebGrid such as ID, Name, Class, etc.
Columns – It is used to specify the columns to be displayed in WebGrid and also allows to set specific Header Text for the columns.
Note: If the columns are not specified then WebGrid will display all columns supplied by its source. It is very similar to the AutoGenerateColumns feature of the ASP.Net GridView.
 
@model IEnumerable<WebGrid_EF_MVC.Customer>
 
@{
    Layout = null;
    WebGrid webGrid = new WebGrid(source: Model);
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .Grid
        {
            border: 1px solid #ccc;
            border-collapse: collapse;
        }
        .Grid th
        {
            background-color: #F7F7F7;
            font-weight: bold;
        }
        .Grid th, .Grid td
        {
            padding: 5px;
            border: 1px solid #ccc;
        }
        .Grid, .Grid table td
        {
            border: 0px solid #ccc;
        }
        .Grid th a, .Grid th a:visited
        {
            color: #333;
        }
    </style>
</head>
<body>
    @webGrid.GetHtml(
        htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
        columns: webGrid.Columns(
                 webGrid.Column("CustomerID", "Customer Id"),
                 webGrid.Column("ContactName", "Customer Name"),
                 webGrid.Column("City", "City"),
                 webGrid.Column("Country", "Country")))
</body>
</html>
 
 
Screenshot
Implement Paging and Sorting in ASP.Net MVC
 
 
Downloads