Lesson 3 - Use Scheduler in Complex Views
- 3 minutes to read
The Lesson 1 - Use Scheduler to Display Appointments in Read-Only Mode document uses a technique that specifies scheduler settings within the view. If your application does not allow users to interact with appointments, this approach works well. However, if the scenario assumes that appointments will be modified, pass the Scheduler settings to the corresponding overloads of the SchedulerExtension.GetAppointmentsToInsert<T>, the SchedulerExtension.GetAppointmentsToUpdate<T> and the SchedulerExtension.GetAppointmentsToRemove<T> methods. Otherwise, appointment display and operations may perform incorrectly.
This approach requires that the SchedulerSettings object is instantiated on the server and passed to the controller and view, so a special helper class (SchedulerSettingsHelper) is implemented to provide the required settings for views and controllers within the application.
@model SchedulerDataObject
@Html.Partial("SchedulerPartial", Model)
@Html.DevExpress().Scheduler(SchedulerSettingsHelper.CommonSchedulerSettings).Bind(Model.Appointments, Model.Resources).GetHtml()
public class SchedulerSettingsHelper
{
static SchedulerSettings commonSchedulerSettings;
public static SchedulerSettings CommonSchedulerSettings
{
get
{
if (commonSchedulerSettings == null)
commonSchedulerSettings = CreateSchedulerSettings();
return commonSchedulerSettings;
}
}
static SchedulerSettings CreateSchedulerSettings()
{
SchedulerSettings settings = new SchedulerSettings();
settings.Name = "scheduler";
settings.CallbackRouteValues = new { Controller = "Home", Action = "SchedulerPartial" };
settings.EditAppointmentRouteValues = new { Controller = "Home", Action = "EditAppointment" };
settings.Storage.Appointments.Assign(SchedulerDataHelper.DefaultAppointmentStorage);
settings.Storage.Resources.Assign(SchedulerDataHelper.DefaultResourceStorage);
settings.Width = 300;
settings.Views.WeekView.Styles.DateCellBody.Height = 50;
settings.Views.MonthView.CellAutoHeightOptions.Mode = AutoHeightMode.FitToContent;
settings.Views.MonthView.AppointmentDisplayOptions.AppointmentAutoHeight = true;
settings.Views.MonthView.AppointmentDisplayOptions.TimeDisplayType = AppointmentTimeDisplayType.Clock;
settings.Views.DayView.Styles.ScrollAreaHeight = 250;
settings.Views.DayView.ShowWorkTimeOnly = true;
settings.Views.DayView.DayCount = 2;
settings.Start = new DateTime(2012, 5, 9);
settings.ActiveViewType = SchedulerViewType.Day;
return settings;
}
}
A controller method that updates appointments is shown in the code snippet below.
@model SchedulerDataObject
@Html.Partial("SchedulerPartial", Model)
@Html.DevExpress().Scheduler(SchedulerSettingsHelper.CommonSchedulerSettings).Bind(Model.Appointments, Model.Resources).GetHtml()
static void UpdateAppointment() {
DBAppointment insertedAppt = SchedulerExtension.GetAppointmentToInsert<DBAppointment>(SchedulerSettingsHelper.CommonSchedulerSettings,
SchedulerDataHelper.GetAppointments(), SchedulerDataHelper.GetResources());
SchedulerDataHelper.InsertAppointment(insertedAppt);
DBAppointment[] updatedAppt = SchedulerExtension.GetAppointmentsToUpdate<DBAppointment>(SchedulerSettingsHelper.CommonSchedulerSettings,
SchedulerDataHelper.GetAppointments(), SchedulerDataHelper.GetResources());
foreach (var appt in updatedAppt) {
SchedulerDataHelper.UpdateAppointment(appt);
}
DBAppointment[] removedAppt = SchedulerExtension.GetAppointmentsToRemove<DBAppointment>(SchedulerSettingsHelper.CommonSchedulerSettings,
SchedulerDataHelper.GetAppointments(), SchedulerDataHelper.GetResources());
foreach (var appt in removedAppt) {
SchedulerDataHelper.RemoveAppointment(appt);
}
}
The view is also changed to obtain settings from the helper object.
@Html.DevExpress().Scheduler(SchedulerSettingsHelper.CommonSchedulerSettings).Bind(Model.Appointments, Model.Resources).GetHtml()