Using DHTMLX Scheduler in MVC

In one of my blogs, I explained how to bind the events in ASP.NET Calender control, you can check the link from here.

In this article, I will explain, how to use DHTMLX scheduler in MVC project, which is similar to the "Binding Events in ASP.NET Calendar Control"

First, we will create a new MVC project. I have created my MVC project  as "Calendar", given below:



Now, our second step is to add DHTMLX Scheduler from NuGet Package, as given below:



Now, search for DHTMLX Scheduler and add to our project..



After adding it in our project, a lot of JavaScript and CSS are automatically added in our project, as given below:



It also adds a Controller, as given below:

 

Now, we need to save the events in our database. For this, we need to create a table as "Events." Here, I have provided the script to create the table:
  1. CREATE TABLE [Events](  
  2.   [id] int IDENTITY(1,1) NOT NULL,  
  3.   [text] nvarchar(256) NULL,  
  4.   [start_date] datetime NOT NULL,  
  5.   [end_date] datetime NOT NULL,  
  6.   PRIMARY KEY (id)  
  7. )  
Now, we will add the ADO.NET Entity data Model to connect and work with the database and table.



Provide the connection details to connect with the database.

 

Give the proper entity name and allow it to save the connection string in Web.Config.



Now, retrieve the table in the ORM pattern.

event

Now, our job from Entity data model is finished.Now go to the calender controller it will have three methods defined; delete all the code from the methods except the method name and structure.

Now  we have only these 3 action methods.
  • Index() - It defines the scheduler configuration.
  • Data() - It loads the data (events) to the scheduler from the database.
  • Save() - It defines,how to create/update/delete the logic. 
Now, create an object for my CodeX entity. Here is the code of Index method. Don't change the code.
  1. public class CalendarController : Controller  
  2.     {  
  3.         CodeXEntities cX = new CodeXEntities();  
  4.         public ActionResult Index()  
  5.         {  
  6.             //Being initialized in that way, scheduler will use CalendarController.Data as a the datasource and CalendarController. Save to process the changes  
  7.             var scheduler = new DHXScheduler(this);  
  8.   
  9.             /* 
  10.              * It's possible to use different actions of the current controller 
  11.              *      var scheduler = new DHXScheduler(this);      
  12.              *      scheduler.DataAction = "ActionName1"; 
  13.              *      scheduler.SaveAction = "ActionName2"; 
  14.              *  
  15.              * Or to specify full paths 
  16.              *      var scheduler = new DHXScheduler(); 
  17.              *      scheduler.DataAction = Url.Action("Data", "Calendar"); 
  18.              *      scheduler.SaveAction = Url.Action("Save", "Calendar"); 
  19.              */  
  20.   
  21.             /* 
  22.              * The default codebase folder is ~/Scripts/dhtmlxScheduler. It can be overriden: 
  23.              *      scheduler.Codebase = Url.Content("~/customCodebaseFolder"); 
  24.              */  
  25.               
  26.    
  27.             scheduler.InitialDate = new DateTime(2012, 09, 03);  
  28.   
  29.             scheduler.LoadData = true;  
  30.             scheduler.EnableDataprocessor = true;  
  31.   
  32.             return View(scheduler);  
  33.         }  
Now, delete all the code given above and write the code, given below, in the data to show the events from our database:
  1. public ContentResult Data()  
  2.        {  
  3.            try  
  4.            {  
  5.                var details = cX.Events.ToList();  
  6.                 
  7.   
  8.   
  9.                return new SchedulerAjaxData(details);  
  10.   
  11.            }  
  12.            catch(Exception ex)  
  13.            {  
  14.                throw ex;  
  15.            }  
  16.             
  17.        }  
This will show the events from the database and show in the scheduler.

Now, go to the save method and write the code, given below, to save the event in the database.
  1. switch (action.Type)  
  2.                {  
  3.                    case DataActionTypes.Insert:  
  4.                        Event EV = new Event();  
  5.                        EV.id = changedEvent.id;  
  6.                        EV.start_date = changedEvent.start_date;  
  7.                        EV.end_date = changedEvent.end_date;  
  8.                        EV.text = changedEvent.text;  
  9.                        cX.Events.Add(EV);  
  10.                        cX.SaveChanges();  
  11.                          
  12.                       
  13.                        break;  
  14. }
Now, save the code and run the Application with the predefined Calender View.
  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6. <html>  
  7. <head>  
  8.     <link href="~/Scripts/dhtmlxScheduler/dhtmlxscheduler.css" rel="stylesheet" />  
  9.     
  10.       
  11.     <title>DHXScheduler initialization sample</title>  
  12.     <style>  
  13.         body  
  14.         {  
  15.             background-color:#eee;  
  16.         }  
  17.     </style>  
  18. </head>  
  19. <body>  
  20.     <div style="height:700px;width:900px;margin:0 auto">  
  21.         @Html.Raw(Model.Render())  
  22.     </div>  
  23. </body>  
  24. </html>  
To test if the events are saving in the database or not, double click on any cell, where you want to write the events.



Afterwards, click the Save button to save the event in the database. It will save the database and will be shown, as given below:

 

Now, if you go and check in the table; the record is saved.

 
Like this, if you want to update and delete any record; write the code, given below, in the update section.
  1. case DataActionTypes.Delete:  
  2.                       var details = cX.Events.Where(x => x.id == id).FirstOrDefault();  
  3.                       cX.Events.Remove(details);  
  4.                       cX.SaveChanges();  
  5.                       
  6.                       break;  
  7.                   default:// "update"    
  8.                       var data = cX.Events.Where(x => x.id == id).FirstOrDefault();  
  9.                       data.start_date = changedEvent.start_date;  
  10.                       data.end_date = changedEvent.end_date;  
  11.                       data.text = changedEvent.text;  
  12.                       cX.SaveChanges();  
  13.   
  14.                       
  15.                       break;  
For updating,  run the project and update the previous event, as given below:



Afterwards, click the update or right symbol to update the record.

 
In this way, we have a delete option and we can also delete the records.



Here is my complete code for the save Action method. 
  1. public ContentResult Save(int? id, FormCollection actionValues)  
  2.           
  3.         {  
  4.               
  5.             var action = new DataAction(actionValues);  
  6.               
  7.             try  
  8.             {  
  9.                 var changedEvent = (Event)DHXEventsHelper.Bind(typeof(Event), actionValues);  
  10.   
  11.        
  12.   
  13.                 switch (action.Type)  
  14.                 {  
  15.                     case DataActionTypes.Insert:  
  16.                         Event EV = new Event();  
  17.                         EV.id = changedEvent.id;  
  18.                         EV.start_date = changedEvent.start_date;  
  19.                         EV.end_date = changedEvent.end_date;  
  20.                         EV.text = changedEvent.text;  
  21.                         cX.Events.Add(EV);  
  22.                         cX.SaveChanges();  
  23.                           
  24.                        
  25.                         break;  
  26.                     case DataActionTypes.Delete:  
  27.                         var details = cX.Events.Where(x => x.id == id).FirstOrDefault();  
  28.                         cX.Events.Remove(details);  
  29.                         cX.SaveChanges();  
  30.                         
  31.                         break;  
  32.                     default:// "update"    
  33.                         var data = cX.Events.Where(x => x.id == id).FirstOrDefault();  
  34.                         data.start_date = changedEvent.start_date;  
  35.                         data.end_date = changedEvent.end_date;  
  36.                         data.text = changedEvent.text;  
  37.                         cX.SaveChanges();  
  38.   
  39.                         
  40.                         break;  
  41.                 }  
  42.             }  
  43.             catch  
  44.             {  
  45.                 action.Type = DataActionTypes.Error;  
  46.             }  
  47.             return (ContentResult)new AjaxSaveResponse(action);  
  48.         }  
Thus, in this way, we can perform CRUD operations inside DHTMLX Scheduler in MVC. Hope this article will help you to understand the concept of DHTMLX Scheduler.

Up Next
    Ebook Download
    View all
    Learn
    View all