jQuery Datatable Date Format And Currency Symbol

This article gives an overview of  how to use jQuery datatable in MVC and format date, currency symbol in jQuery Ajax. I will use jQuery datatable plugin in this demo application and explain how to apply date format and currency symbol. Here is the code.
 
This example requires the following basic understanding
  • Jquery datatable
  • Ajax
  • Bootstrap
  • Entity framework
  • MVC
Step 1
 
There are multiple ways to create a new project in Visual Studio 2019. When you first open Visual Studio, the start window appears, and from there, you can choose to create a new project.
 
jQuery Datatable Date Format And Currency Symbol
 
If the Visual Studio development environment is already open, you can create a new project by choosing File > New > Project on the menu bar or by clicking the New Project button on the toolbar.
 
jQuery Datatable Date Format And Currency Symbol
 
Step 2
 
On the Create a new project page, a list of your recently selected templates appears on the left. The templates are sorted by most recently used.
 
If you're not selecting from the recently used templates, you can filter all available project templates by Language (for example, C#), Platform (for example, Windows or Azure), and Project type (for example, Desktop or Web). You can also enter search text into the search box to further filter the templates, for example, asp.net.
 
jQuery Datatable Date Format And Currency Symbol
 
Select a template and then click Next.
 
Step 3
 
The Configure your new project page has options to name your project (and solution), choose a disk location, and select a Framework version (if applicable to the template you chose).
 
jQuery Datatable Date Format And Currency Symbol
 
Click Create to create the new project.
 
Step 4
 
In create a new ASP.NET Web Application window choose MVC template click on create. Visual studio will generate MVC Web Application project for you.
 
jQuery Datatable Date Format And Currency Symbol
 
Step 5
 
In Visual Studio tools go to NuGet Package Manager and select Manage NuGet Packages for solution.
 
Install the following package for this application:
  • Bootstrap latest version
  • Jquery latest version
  • Datatables latest version
  • Entity framework latest version
jQuery Datatable Date Format And Currency Symbol
 
Step 6
 
Add class Employee in Models folder.
  1. using System;  
  2. using System.ComponentModel.DataAnnotations;  
  3.   
  4. namespace JQueryDatatableDateFormatAndCurrencySymbolMvc_Demo.Models  
  5. {  
  6.     public class Employee  
  7.     {  
  8.         [Key]  
  9.         public int Id { getset; }  
  10.   
  11.         [Required]  
  12.         [StringLength(100)]  
  13.         public string Name { getset; }  
  14.   
  15.         [Required]  
  16.         [StringLength(100)]  
  17.         public string Position { getset; }  
  18.   
  19.         [Required]  
  20.         [StringLength(100)]  
  21.         public string Office { getset; }  
  22.   
  23.         [Required]  
  24.         public int Age { getset; }  
  25.   
  26.         [Required]  
  27.         [DataType(DataType.Date)]  
  28.         public DateTime StartDate { getset; }  
  29.   
  30.         [Required]  
  31.         [DataType(DataType.Currency)]  
  32.         public decimal Salary { getset; }  
  33.     }  
  34. }   
Step 7
 
Create Data Folder and add class ApplicationDbContext for database communication.
  1. using JQueryDatatableDateFormatAndCurrencySymbolMvc_Demo.Models;  
  2. using System.Data.Entity;  
  3.   
  4. namespace JQueryDatatableDateFormatAndCurrencySymbolMvc_Demo.Data  
  5. {  
  6.     public class ApplicationDbContext : DbContext  
  7.     {  
  8.         public ApplicationDbContext() : base("EmployeeDB")  
  9.         {  
  10.   
  11.         }  
  12.   
  13.         public DbSet<Employee> Employees { getset; }  
  14.     }  
  15. }   
Step 8
  1. Enable-migrations
  2. Add-Migration InitialModel
  3. Update-database
Step 9
 
Add bootstrap, jQuery and datatable plugin in _Layout.cshtm
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  6.     <title>@ViewBag.Title</title>  
  7.     @Styles.Render("~/Content/css")  
  8.     @Scripts.Render("~/bundles/modernizr")  
  9.     <link href="~/Content/DataTables/css/dataTables.bootstrap4.min.css" rel="stylesheet" />  
  10. </head>  
  11. <body>  
  12.     <div class="container py-4">  
  13.         @RenderBody()  
  14.     </div>  
  15.     @Scripts.Render("~/bundles/jquery")  
  16.     @Scripts.Render("~/bundles/bootstrap")  
  17.     <script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>  
  18.     <script src="~/Scripts/DataTables/dataTables.bootstrap4.min.js"></script>  
  19.     @RenderSection("scripts", required: false)  
  20. </body>  
  21. </html>   
Step 10
 
Add following code in HomeController
  1. using JQueryDatatableDateFormatAndCurrencySymbolMvc_Demo.Data;  
  2. using System.Linq;  
  3. using System.Web.Mvc;  
  4.   
  5. namespace JQueryDatatableDateFormatAndCurrencySymbolMvc_Demo.Controllers  
  6. {  
  7.     public class HomeController : Controller  
  8.     {  
  9.         private readonly ApplicationDbContext db = new ApplicationDbContext();  
  10.         public ActionResult Index()  
  11.         {  
  12.             return View();  
  13.         }  
  14.   
  15.         public ActionResult GetData()  
  16.         {  
  17.             var employee = db.Employees.ToList();  
  18.             return Json(new { data = employee }, JsonRequestBehavior.AllowGet);  
  19.         }  
  20.   
  21.   
  22.     }  
  23. }   
Step 11
 
Add the following code in Index.cshtml:
  1. @{  
  2.     ViewBag.Title = "Home";  
  3. }  
  4.   
  5. <h3 class="text-center text-uppercase">List of employees</h3>  
  6.   
  7. <table id="example" class="table table-bordered" style="width:100%;">  
  8.     <thead>  
  9.         <tr>  
  10.             <th>Id</th>  
  11.             <th>Name</th>  
  12.             <th>Position</th>  
  13.             <th>Office</th>  
  14.             <th>Age</th>  
  15.             <th>Start Date</th>  
  16.             <th>Salary</th>  
  17.         </tr>  
  18.     </thead>  
  19. </table>  
  20.   
  21. @section scripts{  
  22.     <script type="text/javascript">  
  23.         $(document).ready(function () {  
  24.             $('#example').DataTable({  
  25.                 "ajax": {  
  26.                     "url"'/Home/GetData',  
  27.                     "type"'GET',  
  28.                     "dataType"'json',  
  29.                 },  
  30.                 "columns": [  
  31.                     { 'data''Id' },  
  32.                     { 'data''Name' },  
  33.                     { 'data''Position' },  
  34.                     { 'data''Office' },  
  35.                     { 'data''Age' },  
  36.                     {  
  37.                         'data''StartDate',  
  38.                         'render': function (jsonDate) {  
  39.                             var date = new Date(parseInt(jsonDate.substr(6)));  
  40.                             var month = ("0" + (date.getMonth() + 1)).slice(-2);  
  41.                             return ("0" + date.getDate()).slice(-2) + '-' + month + '-' + date.getFullYear();  
  42.                         }  
  43.                     },  
  44.                     {  
  45.                         'data''Salary',  
  46.                         'render': function (salary) {  
  47.                             return '$' + salary;  
  48.                         }  
  49.                     }  
  50.                 ]  
  51.             });  
  52.         });  
  53.     </script>  
  54. }   
Step 12
 
Now it’s time to build and run your application >> ctrl+F5
jQuery Datatable Date Format And Currency Symbol