jQuery Data Table In ASP.NET MVC

In this blog, you can find the complete source code of jQuery Data Table implemented in ASP.NET MVC Application.

Source Code – HomeController

using JqueryDataTableMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JqueryDataTableMVC.Controllers
{
    public class HomeController : Controller
    {
        employeeDBEntities db = new employeeDBEntities();
        public ActionResult Index()
        {
            var data = db.employees.ToList();
            return View(data);
        }
    }
}

Source Code – Index.cshtml View

@model IEnumerable<JqueryDataTableMVC.Models.employee>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table table-bordered table-hover table-striped" id="MyTable">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.gender)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.designation)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.city)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.salary)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.gender)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.designation)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.city)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.salary)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
                    @Html.ActionLink("Details", "Details", new { id = item.id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.id })
                </td>
            </tr>
        }
    </tbody>
</table>

Source Code – _Layout.cshtml – Master Page File

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />

    <link href="~/Content/jquery.dataTables.min.css" rel="stylesheet" />
    <link href="~/Content/dataTables.bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script src="~/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                </ul>
            </div>
        </div>
    </div>

    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    <script src="~/scripts/jquery-3.4.1.min.js"></script>
    <script src="~/scripts/jquery.dataTables.min.js"></script>
    <script src="~/scripts/dataTables.bootstrap.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        $(document).ready(function ()
        {
            $("#MyTable").dataTable();
        });
    </script>
</body>
</html>

Source Code – employeeDBEntities – Context Class Code

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace JqueryDataTableMVC.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    public partial class employeeDBEntities : DbContext
    {
        public employeeDBEntities()
            : base("name=employeeDBEntities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<employee> employees { get; set; }
    }
}

https://www.mediafire.com/file/tqs2r59fgyw472e/SOURCE_CODE_JQUERY_DATA_TABLE.rar/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *