CRUD Operation In jTable Using MVC

Introduction
 
In this article you will find how can you perform CRUD Operation in jTable using MVC.
 
jTable Introduction
 
According to jtable.org, jTable is a jQuery Plugin that is used to create AJAX based CRUD tables without coding HTML or JavaScript. jTable is really an awesome plugin for AJAX based CRUD operations.
 
Features of jTable
  • Automatically creates table and fill the data into the table using AJAX. 
  • Creates dialog forms for Create, Edit and Delete Operation. 
  • Animation for Create, Edit, Delete.
  • Supports Paging, Sorting, etc.
  • Allow users to selection of Rows and Columns.
  • Allow users to resize Columns.
  • Styling of Table.
  • Show and hide Column.
  • Supports Localization and many more.
Let's perform CRUD operation step by step using MVC.
 
Step 1: First of all create an MVC project by performing the following steps.
  1. Open Visual Studio.
  2. Click on File, New, Project or press CTRL + SHIFT + N,



  3. Then one DilalogBox will open for new project, go to Installed, Templates, Visual C#, then Web and select ASP .NET Web Application and give the name to the project (like jTableMVC) and click on OK button.



  4. After clicking on OK button one more dialog box will be open from that dialog box select MVC as a project template. Select Authentication to Individual User Accounts and then press OK.

     
Step 2: After creating project your complete project file will be as follows in the Solution Explorer.
 
Step 3: Now add a folder for entities. To add the folder to your project here here are the steps, 
  1. Right click on the Project, Add, then New Folder.



  2. Give the name of the folder Entities.
Step 4: Now add a class inside this folder. I am adding with name Marks.CS. To add the class Marks.CS in your project follow the below steps. 
  1. Right Click on the Entities Folder.
  2. Select Add, New, then Class.



  3. Now select Class and give the name of the class as Marks.CS and click on Add button.



Step 5: Now write the following code for Marks.CS,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6.   
  7. namespace jTableMVC.Entities  
  8. {  
  9.     public class Marks  
  10.     {  
  11.         [Key]  
  12.         public string ID { getset; }  
  13.         [Required]  
  14.         public int RollNumber { getset; }  
  15.         [Required]  
  16.         public string Name { getset; }  
  17.         [Required]  
  18.         public string MarksObtained  { getset; }  
  19.         [Required]  
  20.         public string TotalMarks { getset; }  
  21.     }  
  22. }  
Step 6: Now build the project once and go to Models Folder, where you will get "IdentityModels.cs". Open "IdentityModels.cs", and bind Marks.cs to our Model for creating table. Write the following property to ApplicationDbContext class.
 
IdentityModel.cs
  1. using jTableMVC.Entities;  
  2. using Microsoft.AspNet.Identity.EntityFramework;  
  3. using System.Data.Entity;  
  4.   
  5. namespace jTableMVC.Models  
  6. {  
  7.     // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.  
  8.     public class ApplicationUser : IdentityUser  
  9.     {  
  10.     }  
  11.   
  12.     public class ApplicationDbContext : IdentityDbContext<ApplicationUser>  
  13.     {  
  14.         public ApplicationDbContext()  
  15.             : base("DefaultConnection")  
  16.         {  
  17.         }  
  18.         public DbSet<Marks> Marks { getset; }  
  19.     }  
  20. }  
Step 7: Now open package manager console and generate database using the following steps.
  1. Open Package Manager Console.



  2. Type the following command in Package Manager Console.
    1. PM> Enable-Migrations  
  3. Now one folder will be generated with name "Migrations" and there will be file that will be generated with name "Configuration.cs". 
  4. Open "Configuration.cs" and set AutomaticMigrationsEnabled value to true.
  5. Again Open Package Manager Console and type the  following command to generate the database.
    1. PM> update-database  
  6. After firing this command you can open server explorer and there you can see your generated database. Here's the screenshot,


Step 8: Now add Empty MarksController to your project. To Add MarksController you can follow the below steps,
  1. Right Click on Controllers Folder.
  2. Click Add, then Controller



  3. Now one dialog box will open for Add Scaffolding.  Select MVC Controller - Empty.



  4. Now give the name to the Controller as MarksController.
Step 9: Now create some Action methods for CRUD operation in Marks Controller.
  1. Index Action:
    1. public ActionResult Index()  
    2. {  
    3.      return View();  
    4. }  
  2. GetStudentMarks Action:
    1. public JsonResult GetStudentMarks()  
    2. {  
    3.     ApplicationDbContext db = new ApplicationDbContext();  
    4.     try  
    5.     {  
    6.         List<Marks> lstMarks = new List<Marks>();  
    7.         lstMarks = db.Marks.ToList();  
    8.         return Json(new { Result = "OK", Records = lstMarks }, JsonRequestBehavior.AllowGet);  
    9.     }  
    10.     catch (Exception ex)  
    11.     {  
    12.         return Json(new { Result = "ERROR", Message = ex.Message });  
    13.     }  
    14. }  
  3. Create Action:
    1. [HttpPost]  
    2. public JsonResult Create(Marks Model)  
    3. {  
    4.     ApplicationDbContext db = new ApplicationDbContext();  
    5.     try  
    6.     {  
    7.         Model.ID = Guid.NewGuid().ToString();  
    8.         db.Marks.Add(Model);  
    9.         db.SaveChanges();  
    10.         return Json(new { Result = "OK", Record = Model }, JsonRequestBehavior.AllowGet);  
    11.     }  
    12.     catch(Exception ex)  
    13.     {  
    14.         return Json(new { Result = "ERROR", Message = ex.Message });  
    15.     }  
    16. }  
  4. Edit Action:
    1. [HttpPost]  
    2. public JsonResult Edit(Marks Model)  
    3. {  
    4.     ApplicationDbContext db = new ApplicationDbContext();  
    5.     try  
    6.     {  
    7.         db.Entry(Model).State = EntityState.Modified;  
    8.         db.SaveChanges();   
    9.         return Json(new { Result = "OK" }, JsonRequestBehavior.AllowGet);  
    10.     }  
    11.     catch (Exception ex)  
    12.     {  
    13.         return Json(new { Result = "ERROR", Message = ex.Message });  
    14.     }  
    15. }  
  5. Delete Action:
    1. [HttpPost]  
    2. public JsonResult Delete(String ID)  
    3. {  
    4.     ApplicationDbContext db = new ApplicationDbContext();  
    5.     try  
    6.     {  
    7.         Marks marks = db.Marks.Find(ID);  
    8.         db.Marks.Remove(marks);  
    9.         db.SaveChanges();  
    10.         return Json(new { Result = "OK" }, JsonRequestBehavior.AllowGet);  
    11.     }  
    12.     catch (Exception ex)  
    13.     {  
    14.         return Json(new { Result = "ERROR", Message = ex.Message });  
    15.     }  
    16. }  
Step 10: Add View for Index action. You can add View for Index Action using the following steps:
  1. Right Click on Index View.
  2. Click on Add View Option.



  3. Now one Add view DialogBox will be open. Give View name "Index", Select Template Empty (without model) and then click on Add Button.


Step 11: Now Install jTable from NuGet Package Manager by following the steps:
  1. Right click on References and click on Manage Nuget Packages.



  2. Search for jTable.

  3. Install jTable



  4. After Installing you can see jTable folder will be created under Scripts folder.


Step 12: Now add one Java Script File in Scripts folder with name Script.js. Steps to add Java Script file under Scripts folder is as in the following:
  1. Right click on Scripts folder.
  2. Select Add.
  3. Select JavaScript File.
  4. Give the Name of File Script.js
Step 13: After Installing jTable write the following code to index.cshtml of Marks folder.
  1. @{  
  2.     ViewBag.Title = "Marks Details";  
  3. }  
  4.   
  5. <h2>Marks Details</h2>  
  6.   
  7. <div id="MarksTable"></div>  
  8.   
  9. @section scripts{  
  10.     <!--Adding Theme for jTable Grid-->  
  11.     <!--You can choose any type of theme from the themes folder-->  
  12.     <link href="~/Scripts/jtable/themes/metro/blue/jtable.min.css" rel="stylesheet" />  
  13.     @*<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />*@  
  14.     <link href="http://jtable.org/Content/themes/metroblue/jquery-ui.css" rel="stylesheet" type="text/css" />  
  15.     <script src="~/Scripts/jquery-ui-1.9.2.min.js"></script>  
  16.   
  17.     <!--Adding jTable Plugin-->  
  18.     <script src="~/Scripts/jtable/jquery.jtable.min.js"></script>  
  19.   
  20.     <!--Adding our script file-->  
  21.     <script src="~/Scripts/Script.js"></script>  
  22. }  
Step 14: Open Script.js file which we have created in Step 12 and write the following code inside that file:
  1. $(document).ready(function () {  
  2.     $('#MarksTable').jtable({  
  3.         title: 'Marks Detail',  
  4.         actions: {  
  5.             listAction: '/Marks/GetStudentMarks',  
  6.             createAction: '/Marks/Create',  
  7.             updateAction: '/Marks/Edit',  
  8.             deleteAction: '/Marks/Delete'  
  9.         },  
  10.         fields: {  
  11.             ID: {  
  12.                 key: true,  
  13.                 list: false  
  14.             },  
  15.             RollNumber: {  
  16.                 title: 'Roll Number',  
  17.                 width: '15%'  
  18.             },  
  19.             Name: {  
  20.                 title: 'Student Name',  
  21.                 width: '45%'  
  22.             },  
  23.             MarksObtained: {  
  24.                 title: 'Marks Obtained',  
  25.                 width: '15%',  
  26.             },  
  27.             TotalMarks: {  
  28.                 title: 'Total Marks',  
  29.                 width: '15%',  
  30.             }  
  31.         }  
  32.     });  
  33.     $('#MarksTable').jtable('reload');  
  34. });  
Step 15: Now run the project by pressing F5 Key. You will get output as in the following:
  • Initial output when no data will be in the database

  • Add New Record

 
  • Edit Record 

  • Deleting a Record

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com