Lesson 2 - Implement the Insert/Update/Delete Appointment Functionality
- 4 minutes to read
This step-by-step guide demonstrates how to update a simple application created in the Lesson 1 to enable end-users to add new appointments, modify existing appointments and delete them if necessary.
To enable the appointment editing functionality, do the following.
- Step 1. Add an Action Method
- Step 2. Provide Insert, Update, Delete Methods
- Step 3. Modify a View
- Step 4. Observe the Result
Step 1. Add an Action Method
To edit appointments, implement an action method named EditAppointment. It will perform all required modifications, update the scheduler appointment data and pass it to the view for rendering.
A request for editing can be issued if there is 1) a new appointment to insert 2) one or more appointments with modified properties so they should be updated 3) one or more appointments to delete. To handle all these situations, the UpdateAppointment method calls the SchedulerExtension.GetAppointmentsToInsert<T>, SchedulerExtension.GetAppointmentsToUpdate<T> and the SchedulerExtension.GetAppointmentsToRemove<T> MVC Scheduler extension methods.
Methods GetAppointmentsTo* have several overloads intended for use in different situations. This example utilizes a simple method overload that takes the name of a Scheduler, appointment and resource data and mappings as parameters. The method creates a fake scheduler, applies post data and binds the scheduler to data. Since a SchedulerSettings instance is not passed to the method, there might be some settings that are missed in this case. If these settings affect the number of retrieved appointments, this method overload can produce results different from those expected. To overcome this problem, another method overload is implemented that receives a SchedulerSettings instance. It guarantees correct operation but causes difficulties for the developer.
When appointments are retrieved, the helper methods convert them to data objects and update the data source.
Step 2. Provide Insert, Update, Delete Methods
The SchedulerDataHelper class is extended with three methods that handle insert, update and delete operations with appointments.
Note that this sample code uses the standard GetHashCode method to assign a unique ID for a newly created appointment. This technique is not generally recommended since the database can provide its own ID via auto incremented field. If this is the case as it is in the CodeCentral example mentioned above, the ID is overwritten after submitting changes via the DataContext.SubmitChanges method. Consider implementing a proper technique to obtain the unique appointment id.
public class AppointmentDataAccessor
{
public static void InsertAppointment(DBAppointment appt)
{
if (appt == null)
return;
SchedulingDataClassesDataContext db = new SchedulingDataClassesDataContext();
appt.UniqueID = appt.GetHashCode();
db.DBAppointments.InsertOnSubmit(appt);
db.SubmitChanges();
}
public static void UpdateAppointment(DBAppointment appt)
{
if (appt == null)
return;
SchedulingDataClassesDataContext db = new SchedulingDataClassesDataContext();
DBAppointment query = (DBAppointment)(from carSchedule
in db.DBAppointments
where carSchedule.UniqueID == appt.UniqueID
select carSchedule).SingleOrDefault();
query.UniqueID = appt.UniqueID;
query.StartDate = appt.StartDate;
query.EndDate = appt.EndDate;
query.AllDay = appt.AllDay;
query.Subject = appt.Subject;
query.Description = appt.Description;
query.Location = appt.Location;
query.RecurrenceInfo = appt.RecurrenceInfo;
query.ReminderInfo = appt.ReminderInfo;
query.Status = appt.Status;
query.Type = appt.Type;
query.Label = appt.Label;
query.ResourceID = appt.ResourceID;
db.SubmitChanges();
}
public static void RemoveAppointment(DBAppointment appt)
{
SchedulingDataClassesDataContext db = new SchedulingDataClassesDataContext();
DBAppointment query = (DBAppointment)(from carSchedule
in db.DBAppointments
where carSchedule.UniqueID == appt.UniqueID
select carSchedule).SingleOrDefault();
db.DBAppointments.DeleteOnSubmit(query);
db.SubmitChanges();
}
}
Step 3. Modify a View
Now provide a route for the edit appointment action. The code of the Scheduler partial view is shown below.
@Html.DevExpress().Scheduler(
settings => {
settings.Name = "scheduler";
settings.CallbackRouteValues = new { Controller = "Home", Action = "SchedulerPartial" };
settings.EditAppointmentRouteValues = new { Controller = "Home", Action = "EditAppointment" };
settings.Storage.Appointments.Assign(MVCSchedulerEditable.Models.SchedulerStorageProvider.DefaultAppointmentStorage);
settings.Storage.Resources.Assign(MVCSchedulerEditable.Models.SchedulerStorageProvider.DefaultResourceStorage);
settings.Storage.EnableReminders = true;
settings.ActiveViewType = SchedulerViewType.FullWeek;
settings.Start = new DateTime(2015, 4, 18);
}).Bind(Model.Appointments, Model.Resources).GetHtml()
Step 4. Observe the Result.
Run the project. You will see the scheduler with editable appointments on a page as illustrated in the following image.