Implement jQuery Datatable in ASP.NET MVC application


Here in this article, we will see how to Implement jQuery Datatable in ASP.NET MVC application.
DataTables is a most powerful and easy to use jQuery plugin for display tabular data (table listings) with support for Pagination, searching, State saving, Multi-column sorting with data type detection and lots more with ZERO or minimal configuration. Most important It is open source.

Do you have AngularJS application? Please visit datatables in AngularJS and mvc in order to implement datatables in angular application.

Before this article, I have explained How to use Telerik Grid in MVC4 application , How to display database data in webgrid in mvc and more about gridview for display tabular data in our application.

Here we will see followings with ASP.NET MVC as server side...
  1. Part 1: Implement jQuery Datatable (Basic initialization) in ASP.NET MVC application.
  2. Part 2: jQuery Datatable server side pagination and sorting in ASP.NET MVC
  3. Part 3: Implement custom multicolumn server-side filtering in jQuery dataTables
  4. Next article coming soon...
DataTables is very easy to use. We just need to add 1 CSS file, and 1 js file and 3 lines of jquery code as below to start.
                $(document).ready(function () {
you can check the below code...
              <link rel="stylesheet" type="text/css" href="">
            <div style="width:400px; margin:0 auto">
              <table id="myTable" >
              <script type="text/javascript" charset="utf8" src=""></script>
              <script type="text/javascript" charset="utf8" src=""></script>

But it will be useful when I will fetch data from server side for display in jQuery DataTable. So, Here we will see How to fetch data from server side and display in jQuery DataTable.

Just follow the following steps in order Implement jQuery Datatable (Basic initialization) in ASP.NET MVC application.

Step-1: Create New Project.

Go to File > New > Project > Select MVC4 web application > Entry Application Name > Click OK > Select Basic > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.
Here I have added a database for store some location information in our database for show in the google map.

Step-3: Create a table.

Here I will create 1 table (as below) for store data.
Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used 1 table as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Create a Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Here I have created a controller "HomeController"

Step-6: Add new action into the controller to get the view where we will implement jQuery DataTable

Here I have added "Index" Action into "Home" Controller. Please write this following code
public ActionResult Index()
    return View();

Step-7: Add view for the action (here "Index") & design.

Right Click on Action Method (here right click on Index action) > Add View... > Enter View Name > Select View Engine (Razor) > Add.
Complete HTML code
    ViewBag.Title = "Index";

<h2>Part 1 : Implement jQuery Datatable in ASP.NET MVC</h2>
<div style="width:90%; margin:0 auto;">
    <table id="myTable">
                <th>Employee Name</th>
                <th>Postal Code</th>
    tr.even {
    background-color: #F5F5F5 !important;
@* Load datatable css *@
<link href="//" rel="stylesheet" />
@* Load datatable js *@
@section Scripts{
    <script src="//"></script>
        $(document).ready(function () {
                "ajax": {
                    "url": "/home/loaddata",
                    "type": "GET",
                    "datatype": "json"
                "columns" : [
                        { "data": "ContactName", "autoWidth": true },
                        { "data": "CompanyName", "autoWidth": true },
                        { "data": "Phone", "autoWidth": true },
                        { "data": "Country", "autoWidth": true },
                        { "data": "City", "autoWidth": true },
                        { "data": "PostalCode", "autoWidth": true }

Step-8: Add another action (here "loaddata") for fetch data from the database.

public ActionResult loaddata()
    using (MyDatabaseEntities dc = new MyDatabaseEntities())
        // dc.Configuration.LazyLoadingEnabled = false; // if your table is relational, contain foreign key
        var data = dc.Customers.OrderBy(a => a.ContactName).ToList();
        return Json(new { data = data }, JsonRequestBehavior.AllowGet);

Step-9: Run Application.

Yahoooooo! We have done.... BUT here in this article we fetched all the data from server at once and done pagination, sorting, instant searching at client side. This can be a performance issue fetching a large amount of data from server side at once. To resolve this performance issue, we can integrate server-side pagination, sorting and searching. In the next article, I have implemented jQuery DataTable server side pagination and sorting in ASP.NET MVC application

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms,, sql server, entity framework, Ajax, Jquery, web api, web service and more.