We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to perform CRUD operation on Schedule?

The following steps shows the way to perform CRUD operation on appointments present within schedule.

 

Step 1:  Create a schedule default sample using this link and bind the appointment data through URL as below code.

<ej-schedule id="Schedule1" width="100%" height="525px" current-date="new DateTime(2014, 12, 5)">

    <e-appointment-settings apply-time-offset="false" id="Id" subject='"Subject"' start-time='"StartTime"' end-time='"EndTime"' all-day='"AllDay"' recurrence='"Recurrence"' recurrence-rule='"RecurrenceRule"'>

        <e-datamanager url="Home/GetData" crud-url="Home/Batch" adaptor="UrlAdaptor"></e-datamanager>

    </e-appointment-settings>

</ej-schedule>

 

Step 2: Define the GetData and Batch function in controller page to retrieve the appointments initially from database and to handle the CRUD operations respectively.

public List<DefaultSchedule> GetData()

        {

 

            List<DefaultSchedule> datas = _context.DefaultSchedule.Take(500).ToList();

            return datas;

        }

 

        public List<DefaultSchedule> Batch([FromBody] EditParams param)

        {

            if (param.action == "insert" || (param.action == "batch" && (param.added.Count>0))) // this block of code will execute while inserting the appointments

            {

                DefaultSchedule appoint = new DefaultSchedule();

                object result;

                if (param.action == "insert")

                {

                    var value = param.value;

                    foreach (var fieldName in value.GetType().GetProperties())

                    {

                        var newName = fieldName.ToString().Split(null);

                        if (newName[1] == "Id") result = (_context.DefaultSchedule.ToList().Count > 0 ? _context.DefaultSchedule.ToList().Max(p => p.Id) : 1) + 1;

                        else if (newName[1] == "StartTime" || newName[1] == "EndTime") result = Convert.ToDateTime(fieldName.GetValue(value));

                        else result = fieldName.GetValue(value);

                        fieldName.SetValue(appoint, result);

                    }

                    _context.DefaultSchedule.Add(appoint);

                }

                else

                {

                    foreach (var item in param.added.Select((x, i) => new { Value = x, Index = i }))

                    {

                        var value = item.Value;

                        foreach (var fieldName in value.GetType().GetProperties())

                        {

                            var newName = fieldName.ToString().Split(null);

                            if (newName[1] == "Id") result = (_context.DefaultSchedule.ToList().Count > 0 ? _context.DefaultSchedule.ToList().Max(p => p.Id) : 1) + 1 + item.Index;

                            else if (newName[1] == "StartTime" || newName[1] == "EndTime") result = Convert.ToDateTime(fieldName.GetValue(value));

                            else result = fieldName.GetValue(value);

                            fieldName.SetValue(appoint, result);

                        }

                        _context.DefaultSchedule.Add(appoint);

                    }

                }

                _context.SaveChanges();

            }

            if ((param.action == "remove") || (param.action == "batch" && (param.deleted.Count > 0))) // this block of code will execute while removing the appointment

            {

                if (param.action == "remove")

                {

                    DefaultSchedule app = _context.DefaultSchedule.Where(c => c.Id == Convert.ToInt32(param.key)).FirstOrDefault();

                    if (app != null) _context.DefaultSchedule.Remove(app);

                }

                else

                {

                    foreach (var a in param.deleted)

                    {

                        var app = _context.DefaultSchedule.ToList().Where(c => c.Id == Convert.ToInt32(a.Id)).FirstOrDefault();

                        if (app != null) _context.DefaultSchedule.Remove(app);

                    }

                }

                _context.SaveChanges();

            }

            if (param.action == "update" || (param.action == "batch" && (param.changed.Count > 0))) // this block of code will execute while updating the appointment

            {

                var value = param.action == "update" ? param.value : param.changed[0];

                var filterData = _context.DefaultSchedule.Where(c => c.Id == Convert.ToInt32(value.Id));

                if (filterData.Count() > 0)

                {

                    DefaultSchedule appoint = _context.DefaultSchedule.Single(A => A.Id == Convert.ToInt32(value.Id));

                    appoint.StartTime = Convert.ToDateTime(value.StartTime);

                    appoint.EndTime = Convert.ToDateTime(value.EndTime);

                    appoint.Subject = value.Subject;

                    appoint.Recurrence = value.Recurrence;

                    appoint.AllDay = value.AllDay;

                    appoint.RecurrenceRule = value.RecurrenceRule;

                }

                _context.SaveChanges();

            }

            List<DefaultSchedule> datas = _context.DefaultSchedule.Take(500).ToList();

            return datas;

        }

    }

 

Sample: http://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample_CRUD-2059963238

 

Run the above sample and the CRUD operations of appointments on the scheduler will reflect successfully on database.

Article ID: Published Date: Last Revised Date: Platform: Control:
8488 02/26/2018 02/26/2018 ASP.NET Core Schedule
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.