|Visual Studio version
all browsers supported by RadControls
This project, inspired by this Telerik blog post
, shows how to use an ASP.Net MVC controller instead of a web service to provide data access to a Telerik Scheduler control. This will allow you to maintain a consistent development approach throughout your project, as well as give you the ability to use the Authorize attribute for user access control on the service methods.
The controller is fairly simple and shouldn't require too much explanation. It can be found in Controllers\SchedulerServiceController.cs in the demo project. It has methods that match those found in the .asmx file found in the MVC demo project
, though return types are JsonResult instead of the various IEnumerable types returned from the .asmx service. The methods are decorated with two attributes, HttpPost and JsonParameterFilter. HttpPost should be familiar and if not can be found in the ASP.Net MVC documentation. The JsonParameterFilter is taken from the above mentioned Telerik blog post and is used to convert a JSON string from the browser to a .Net object. I changed the class to accept multiple JSON strings and output values, as a number of the service methods for the Scheduler have more than one parameter. To keep the demo simple and avoid the need for a database, data persistence is handled Telerik's XmlSchedulerProvider
. Returned results are serialized to JSON and formatted for ASP.Net Ajax as noted in the Telerik blog post. Finally, in the view that hosts the Scheduler control, the control's markup is set to use the controller like this
I found that when used without user authentication, the Scheduler control and controller worked without any problem. However, when using authentication and the Authorize attribute, the GetResources call would fail. This is mentioned in the Telerik help file here
. Since MVC views don't have postbacks and the associated event handling, I created my own server control that inherits from the Scheduler control and then overrode the OnResourcesPopulating method to implement the solution mentioned in the help file.
An additional issue discovered while copying the authentication cookie was that a session cookie would cause the web server to hang. According to this post
, when the Scheduler control calls GetResources it uses a session id of its own, and it seems that including another session cookie causes problems. The simple solution was to copy the authentication cookie and ignore the session cookie. The downside to this is that items stored in the Session object during the GetResources call will be lost on the next call.
You should also be sure that you have authenticated the user before you load the page that contains the Scheduler control. When the Scheduler control loads and calls the controller's methods, it is expecting a JSON result; any other result will cause a crash. Comment out the Authorize attribute on Controllers\ScheduleController.cs and then try viewing the Schedule page without logging in to see this in action. In the demo I have used separate controllers: Controllers\ScheduleController.cs which returns the view and Controllers\SchedulerServiceController.cs which handles the web service methods. These could be combined into one controller on which you could use the Authorize attribute to ensure all the methods are authorized appropriately.
To accommodate multiple method parameters, I changed the JsonParameterFilterAttribute to use string and Type arrays for the Parameters and TargetTypes properties, respectively. Be sure that you have a parameter name and a target type specified for each parameter on each or your service methods. In other words, Parameters and TargetTypes need to have the same number of elements. See the usage in Controllers\SchedulerServiceController.cs, particularly on a method with multiple parameters such as DeleteAppointment.
To keep the demo simple and avoid the need to deal with a database, the authentication info is hard-coded in the web.config file. To logon, use
username = admin
password = password
(these values have been added to the LogOn.aspx page so the logon form will be pre-populated)
Summary of steps/tips/things learned
1. Create controller
2. Add service methods that return JsonResult
3. Decorate service methods with JsonParameterFilter attribute, specifying Parameter names and Target Types
4. Set Scheduler's markup to use a web service, with the path pointing at the controller
5. The Scheduler doesn't automatically include the authentication cookie when calling GetResources, you'll need to do it manually
6. Including a session cookie when calling GetResources causes problems, be sure to NOT include it
7. Since you're not including the session cookie when calling GetResources, be sure you don't need to retrieve anything stored in the session while inside GetResources
8. If using the Authorize attribute on your service methods, authenticate the user before displaying a view containing the Scheduler
9. Be sure to include a name and type for each parameter when using the JsonParameterFilterAttribute