Saving and Updating Gantt Data Using Code First Approach MVC 5 ASP.NET

How do I save gantt data to the database using the database code first approach? Below is a sample application that I would like to save data and update the gantt data from (performing CRUD operations) using code first approach. Please kindly do assist.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Syncfusion.JavaScript;
using YothingoSprint1.Models;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace YothingoSprint1
{
    public class TaskData
    {
        public int TaskId { get; set; }
        public string TaskName { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime BaseLineStartDate { get; set; }
        public DateTime BaseLineEndDate { get; set; }
        public string ParentId { get; set; }
        public int Duration { get; set; }
        public int PercentDone { get; set; }
        public string Predecessor { get; set; }

        public partial class GanttController : Controller
    {

        ApplicationDbContext db = new ApplicationDbContext();

        public ActionResult GanttFeatures()
        {
            ViewBag.dataSource = db.GanttFeatureDatas.ToList();
            ViewBag.resources = ResourceList.GetResources();
            return View();
        }

        [HttpPost()]
        public ActionResult Update(List<TaskData> Tasks)
        {
            foreach (TaskData Task in Tasks)
            {
                int Id = Task.TaskId;
                GanttFeatureData data = db.GanttFeatureDatas.First(sv => sv.Id == Id);

                data.Id = Task.TaskId;
                data.Name = Task.TaskName;
                data.StartDate = Task.StartDate;
                data.BaselineStartDate = Task.BaseLineStartDate;
                data.BaselineEndDate = Task.BaseLineEndDate;
                data.Duration = Task.Duration;
                //data.ParentId = Task.ParentId;
                data.Predescessor = Task.Predecessor;
                data.PercentDone = Task.PercentDone;
            }
            db.SaveChanges();
            return Json(true, JsonRequestBehavior.AllowGet);

        }

        [HttpPost()]
        public ActionResult Add(TaskData Task)
        {

            GanttFeatureData data = new GanttFeatureData
            {
                data.Id = Task.TaskId;
                data.Name = Task.TaskName;
                data.StartDate = Task.StartDate;
                data.BaselineStartDate = Task.BaseLineStartDate;
                data.BaselineEndDate = Task.BaseLineEndDate;
                data.Duration = Task.Duration;
                //data.ParentId = Task.ParentId;
                data.Predescessor = Task.Predecessor;
                data.PercentDone = Task.PercentDone;

            };
            db.GanttFeatureDatas.Add(data);
            db.SaveChanges();
            return Json(Task, JsonRequestBehavior.AllowGet);
        }

        [HttpPost()]
        public ActionResult Delete(TaskData Task)
        {

            int id = Task.TaskId;
            GanttFeatureData GanttRow = db.GanttFeatureDatas.Find(id);
            db.GanttFeatureDatas.Remove(GanttRow);
            db.SaveChanges();
            return Json(Task, JsonRequestBehavior.AllowGet);
        }


    }
    public class Resource
        {
            public int ResourceId { get; set; }
            public string ResourceName { get; set; }
        }
        public class ResourceList
        {
            public static List<Resource> GetResources()
            {
                List<Resource> ResourceCollection = new List<Resource>();
                ResourceCollection.Add(new Resource() { ResourceId = 1, ResourceName = "Assembly Manager" });
                ResourceCollection.Add(new Resource() { ResourceId = 2, ResourceName = "Admin" });
                ResourceCollection.Add(new Resource() { ResourceId = 3, ResourceName = "Quality Inspector" });
                ResourceCollection.Add(new Resource() { ResourceId = 4, ResourceName = "Inventory Manager" });
                return ResourceCollection;
            }
        }

}

 public class GanttFeatureData
    {
        public object StartDate { get; set; }
        public int Id { get; set; }
        public string Name { get; set; }
        public int Duration { get; set; }
        public int PercentDone { get; set; }
        public List<GanttFeatureData> Children { get; set; }
        public string Predescessor { get; set; }
        public DateTime BaselineStartDate { get; set; }
        public DateTime BaselineEndDate { get; set; }
        public List<int> Resources { get; set; }
    }



7 Replies

JA Jesus Arockia Sankaran S Syncfusion Team October 31, 2018 04:10 AM UTC

Hi Nosihle, 
Thanks for contacting Syncfusion support. 
It is possible to pass all the CRUD operations over the Gantt records from client side to server side with the help of ActionComplete client side event which can be updated to the database using code first approach.  
Please find the below code example for details with reference sample. 
Code Snippet
[CSHTML]  
@(Html.EJ().Gantt("Gantt"). 
ClientSideEvents(eve=>{ 
          eve.ActionComplete("ActionComplete");           
      }) 
//… 
) 
<script type="text/javascript">      
        
function ActionComplete(args) {             
            if (args.requestType == "indent" || args.requestType == "outdent" || args.requestType == "recordUpdate" || (args.requestType === 'save' && args.modifiedRecord) || args.requestType == "drawConnectorLine") { 
                var ganttRec = []; 
                // Dialog Editing 
                if (args.requestType == "save") 
                    data = args.modifiedRecord; 
                else if (args.requestType == "drawConnectorLine") 
                    data = args.currentRecord; 
                else 
                    data = args.data; // Cell Editing 
                if (!ej.isNullOrUndefined(data.ResourceID)) 
                    data.ResourceID = UpdateResourceID(data.ResourceID); 
                ganttRec.push(data); 
                if (args.updatedRecords && args.updatedRecords.length) 
                    ganttRec = ganttRec.concat(args.updatedRecords); 
                updateModifiedGanttRecords(ganttRec); 
            } 
                //Newly Added Record is obtained here , which can be updated to database 
            else if (args.requestType == "save" && args.addedRecord) { 
                var data = args.addedRecord.item; 
                if (!ej.isNullOrUndefined(data.ResourceID)) 
                    data.ResourceID = UpdateResourceID(data.ResourceID); 
                $.ajax({ 
                    type: "POST", 
                    url: '/Gantt/Add', 
                    contentType: "application/json; charset=utf-8", 
                    data: JSON.stringify(data), 
                    dataType: "json", 
                }); 
                // args.updatedRecords will have the array of dependent hierarchy of modified 
                //parents record when add new record as child of another record. 
                if (args.updatedRecords && args.updatedRecords.length) 
                    updateModifiedGanttRecords(args.updatedRecords); 
            } 
                //To update the database on delete action 
            else if (args.requestType == "delete") { 
                var data = args.data.item; 
                if (!ej.isNullOrUndefined(data.ResourceID)) 
                    data.ResourceID = UpdateResourceID(data.ResourceID); 
                $.ajax({ 
                    type: "POST", 
                    url: '/Gantt/Delete', 
                    contentType: "application/json; charset=utf-8", 
                    data: JSON.stringify(data), 
                    dataType: "json", 
                }); 
                if (args.data.hasChildRecords) { 
                    deleteChildRecords(args.data); 
                } 
                if (args.updatedRecords && args.updatedRecords.length) 
                    updateModifiedGanttRecords(args.updatedRecords); 
            } 
        } 
 
        function updateModifiedGanttRecords(records) { 
            var modifiedRecord = []; 
            if (records && records.length) { 
                var length = records.length; 
                for (var i = 0; i < length; i++) 
                    modifiedRecord.push(records[i].item); 
            } 
            $.ajax({ 
                type: "POST", 
                url: '/Gantt/Update', 
                contentType: "application/json; charset=utf-8", 
                data: modifiedRecord, 
                dataType: "json", 
            }); 
        } 
        //Delete inner level child records 
        function deleteChildRecords(record) { 
            var childRecords = record.childRecords, 
            length = childRecords.length, 
            count, currentRecord; 
            for (count = 0; count < length; count++) { 
                currentRecord = childRecords[count]; 
                var data = currentRecord.item; 
                $.ajax({ 
                    type: "POST", 
                    url: '/Gantt/Delete', 
                    contentType: "application/json; charset=utf-8", 
                    data: data, 
                    dataType: "json", 
                }); 
                if (currentRecord.hasChildRecords) { 
                    deleteChildRecords(currentRecord); 
                } 
            } 
        }          
</script> 
 
Please get back to us if you need any further assistance.  
Regards,  
Jesus Arockia Sankaran S 
 
 
 





NO Nosihle October 31, 2018 08:38 PM UTC

Thank you so much, this was helpful.


JA Jesus Arockia Sankaran S Syncfusion Team November 1, 2018 11:48 AM UTC

Hi Nosihle,  
Thanks for your update. 
Please get back to us if you need any further assistance.  
Regards,  
Jesus Arockia Sankaran S 



PR Projectier February 13, 2020 05:58 AM UTC

Hello Team,
I'm getting issue on the same project. Error mention below

The call is ambiguous between the following methods or properties: 'Syncfusion.JavaScript.GanttPropertiesBuilder.Datasource(string)' and 'Syncfusion.JavaScript.GanttPropertiesBuilder.Datasource(System.Action<Syncfusion.JavaScript.DataSourceBuilder>)'



KR Karthikeyan Raja Syncfusion Team February 14, 2020 06:23 PM UTC

Hi Nosihle, 

We analyzed the query and the reported issue is due to improper data mapping in the dataSource property. We need to use the same variable name for the data source that is used in the controller. Please find the code snippet below, 
[Controller.cs] 
public ActionResult Index() 
{ 
    var data = GanttTaskData.GetData(); 
    ViewBag.dataSource = data; 
    return View(); 
} 
 
[Index.cshtml] 
@(Html.EJ().Gantt("Gantt") 
//……. 
   .Datasource(ViewBag.dataSource) 
) 
 
Please get back to us, if you need any further assistance on this. 

Regards, 
Karthikeyan Raja 



KR Krunal March 2, 2021 05:13 PM UTC

What is data fails to save to database using ajax call due to any reason e.g. connectivity issue, exception on server side, etc. In this case, UI should pop up error message informing user that something has gone wrong with saving data to database. Also, in case of such error, UI should not reflect the intended changes i.e. if I'm saving record then save dialog should not close and remain open only.

I understand actionBegin should be treated as preview event where attempt should be made to write data to database and if error occurs during data save then there should be way to prevent actionComplete event to fire.

So question basically is how to handle server side save errors and notify user about errors?


PP Pooja Priya Krishna Moorthy Syncfusion Team March 3, 2021 04:01 PM UTC

Hi Krunal, 
We can handle server-side errors by using actionFailure event. Please refer the below documentation link for more details. 
Please get back to us if you need more information on this. 
  
Regards, 
Pooja K

Loader.
Up arrow icon