Since it doesn’t seem to be possible to reload the data when the user changes the date if it has already been retrieved once, it is very important that an updated event stays updated when the user changes the date.
In our sample project the user can update the text of an event, go to another date which calls the Data action on the controller, and then go back to the first date which is cached, but the updated event text is then reset and does not display the changed text anymore.
What are we missing?
public class HomeController : Controller
{
public ActionResult Index()
{
//Being initialized in that way, scheduler will use CalendarController.Data as a the datasource and CalendarController.Save to process changes
var scheduler = new DHXScheduler(this);
scheduler.InitialDate = DateTime.Now.Date;
var vehicleList = new List<Vehicle>();
var vehicle1 = new Vehicle() { key = "1", label = "Vehicle 1" };
vehicleList.Add(vehicle1);
var vehicle2 = new Vehicle() { key = "2", label = "Vehicle 2" };
vehicleList.Add(vehicle2);
var vehicle3 = new Vehicle() { key = "3", label = "Vehicle 3" };
vehicleList.Add(vehicle3);
var timeline = new TimelineView("myTimeline", "vehicle_id");
timeline.AddOptions(vehicleList);
timeline.RenderMode = TimelineView.RenderModes.Bar;
scheduler.Views.Add(timeline);
scheduler.PreventCache();
scheduler.Calendars.AttachMiniCalendar();
scheduler.LoadData = true;
scheduler.EnableDynamicLoading(SchedulerDataLoader.DynamicalLoadingMode.Day);
scheduler.EnableDataprocessor = true;
scheduler.InitialView = timeline.Name;
return View(scheduler);
}
[OutputCache(Duration = 0, VaryByParam = "*")]
public ContentResult Data()
{
var events = new List<CalendarEvent>
{
new CalendarEvent
{
id = 1,
text = "Sample Event",
start_date = DateTime.Now.AddHours(3),
end_date = DateTime.Now.AddHours(6),
vehicle_id = "1"
},
new CalendarEvent
{
id = 2,
text = "New Event",
start_date = DateTime.Now,
end_date = DateTime.Now.AddHours(2),
vehicle_id = "2"
},
new CalendarEvent
{
id = 3,
text = "Multiday Event",
start_date = DateTime.Now.AddHours(1),
end_date = DateTime.Now.AddHours(2),
vehicle_id = "3"
}
};
var sortedData = (from e in events
orderby e.start_date
select e).ToList();
var data = new SchedulerAjaxData(sortedData);
return (ContentResult)data;
}
public ContentResult Save(int? id, FormCollection actionValues)
{
var events = new List<CalendarEvent>
{
new CalendarEvent
{
id = 1,
text = "Sample Event",
start_date = DateTime.Now.AddHours(3),
end_date = DateTime.Now.AddHours(6),
vehicle_id = "1"
},
new CalendarEvent
{
id = 2,
text = "New Event",
start_date = DateTime.Now,
end_date = DateTime.Now.AddHours(2),
vehicle_id = "2"
},
new CalendarEvent
{
id = 3,
text = "Multiday Event",
start_date = DateTime.Now.AddHours(1),
end_date = DateTime.Now.AddHours(2),
vehicle_id = "3"
}
};
var action = new DataAction(actionValues);
try
{
var changedEvent = (CalendarEvent)DHXEventsHelper.Bind(typeof(CalendarEvent), actionValues);
switch (action.Type)
{
case DataActionTypes.Insert:
//do insert
action.TargetId = changedEvent.id;//assign postoperational id
break;
case DataActionTypes.Delete:
//do delete
break;
default:// "update"
//do update
var eventToUpdate = events.SingleOrDefault(ev => ev.id == action.SourceId);
DHXEventsHelper.Update(eventToUpdate, changedEvent, new List<string>() { "id" });
break;
}
}
catch
{
action.Type = DataActionTypes.Error;
}
return (ContentResult)new AjaxSaveResponse(action);
}
}