Articles in this section
Category / Section

How to perform CRUD operation on ASP.NET Core Schedule?

6 mins read

The following steps shows the way to perform CRUD operation on appointments present within ASP.NET Core 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: https://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.

Conclusion

I hope you enjoyed learning how to perform CRUD operation on ASP.NET Core Schedule.

You can refer to our ASP.NET Core Schedule feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core Schedule example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied