This is a counterpart of the How to filter resources in ASPxScheduler via ASPxListBox code example but for ASP.NET MVC platform.
As a starting point, we are using the data-bound Scheduler in the partial view (see Note section in the Callback-Based Functionality help article). You can find a similar logic in Lesson 2 - Implement Insert-Update-Delete Appointment Functionality. But our code is more extensible and reliable for the following reason.
We use the SchedulerHelper class to initialize Scheduler settings for both view and controller. This allows us to implement a reliable solution according to the Lesson 3 - Use Scheduler in Complex Views. This is preferable implementation, which should operate correctly in any possible scenarios.
It is not necessary to isolate CheckBoxList in a partial view because it is not operating in callback mode. Thus, we place it in the main view:
@model SchedulerFilterResourcesDataLevelMvc.Models.SchedulerDataObject
<script type="text/javascript">
// <![CDATA[
function OnSelectedIndexChanged(s, e) {
scheduler.PerformCallback();
}
function OnBeginCallback(s, e) {
e.customArgs['SelectedResources'] = cbResources.GetSelectedValues().join(',');
}
// ]]>
</script>
<table>
<tr>
<td valign="top">
@Html.DevExpress().CheckBoxList(settings => {
settings.Name = "cbResources";
settings.Width = System.Web.UI.WebControls.Unit.Pixel(200);
settings.Properties.ClientSideEvents.SelectedIndexChanged = "OnSelectedIndexChanged";
settings.Properties.ValueField = "ID";
settings.Properties.TextField = "Model";
}).BindList(Model.Resources).GetHtml()
</td>
<td>
@Html.Partial("SchedulerPartial", Model)
</td>
</tr>
</table>
The OnBeginCallback
function name is assigned to the settings.ClientSideEvents.BeginCallback
attribute of the Scheduler settings initialized in the SchedulerHelper class. Thus, this function is called before callback occurs. We pass parameters to the corresponding controller's action in this function as described in thePassing Values to Controller Action Through Callbacks help topic. This action is defined as follows:
public ActionResult SchedulerPartial() {
return PartialView("SchedulerPartial", SchedulerDataHelper.GetDataObject(GetSelectedResourceIds()));
}
...
List<int> GetSelectedResourceIds() {
string request = (Request.Params["SelectedResources"] != null) ? (Request.Params["SelectedResources"]) : string.Empty;
return (request != string.Empty) ? request.Split(',').Select(n => Convert.ToInt32(n)).ToList<int>() : new List<int>();
}
The SchedulerDataHelper.GetDataObject()
method is implemented so that the returned object resources are filtered by a list of Ids passed to this method. Note that we use this method in the EditAppointment
action either.
- SchedulerHelper.cs (VB: SchedulerHelper.vb)
- HomeController.cs (VB: HomeController.vb)
- Scheduling.cs (VB: Scheduling.vb)
- Index.cshtml
- SchedulerPartial.cshtml
(you will be redirected to DevExpress.com to submit your response)