Introduction

In my previous article working with scheduler widget in Kendo UI I explained how to implement the Kendo Scheduler in a web application. From this article we will learn how to remote bind the Kendo Scheduler using ASP.NET WEB PI and Entity Framework.

Remote Binding the Kendo Scheduler

  • Open Visual Studio and create a new project.
  • Select File, New and then Project.
  • Select Web in the installed template and select ASP.NET Web Application.
  • Provide the name for the project and click OK, as in the following figures.


My database schema is as in the following figure:



My table structure is as in the following figure:
I am using the Entity Framework with Database First approach, so the Entity Framework builds default model classes and context classes.

Here is my code in EventController Class:
  1. public class EventsController : ApiController
  2. {
  3. private EventEntities db = new EventEntities();
  4. // GET: api/Events
  5. public IQueryable<Event> GetEvents()
  6. {
  7. return db.Events;
  8. }
  9. // GET: api/Events/5
  10. [ResponseType(typeof(Event))]
  11. public IHttpActionResult GetEvent(int id)
  12. {
  13. Event @event = db.Events.Find(id);
  14. if (@event == null)
  15. {
  16. return NotFound();
  17. }
  18. return Ok(@event);
  19. }
  20. // PUT: api/Events/5
  21. [ResponseType(typeof(void))]
  22. public IHttpActionResult PutEvent(int id, Event @event)
  23. {
  24. if (!ModelState.IsValid)
  25. {
  26. return BadRequest(ModelState);
  27. }
  28. if (id != @event.TaskID)
  29. {
  30. return BadRequest();
  31. }
  32. db.Entry(@event).State = EntityState.Modified;
  33. try
  34. {
  35. db.SaveChanges();
  36. }
  37. catch (DbUpdateConcurrencyException)
  38. {
  39. if (!EventExists(id))
  40. {
  41. return NotFound();
  42. }
  43. else
  44. {
  45. throw;
  46. }
  47. }
  48. return StatusCode(HttpStatusCode.NoContent);
  49. }
  50. // POST: api/Events
  51. [ResponseType(typeof(Event))]
  52. public IHttpActionResult PostEvent(Event @event)
  53. {
  54. if (!ModelState.IsValid)
  55. {
  56. return BadRequest(ModelState);
  57. }
  58. db.Events.Add(@event);
  59. db.SaveChanges();
  60. return CreatedAtRoute("DefaultApi", new { id = @event.TaskID }, @event);
  61. }
  62. // DELETE: api/Events/5
  63. [ResponseType(typeof(Event))]
  64. public IHttpActionResult DeleteEvent(int id)
  65. {
  66. Event @event = db.Events.Find(id);
  67. if (@event == null)
  68. {
  69. return NotFound();
  70. }
  71. db.Events.Remove(@event);
  72. db.SaveChanges();
  73. return Ok(@event);
  74. }
  75. protected override void Dispose(bool disposing)
  76. {
  77. if (disposing)
  78. {
  79. db.Dispose();
  80. }
  81. base.Dispose(disposing);
  82. }
  83. private bool EventExists(int id)
  84. {
  85. return db.Events.Count(e => e.TaskID == id) > 0;
  86. }
  87. }

Check the API services using the POSTMAN/Fiddler as in the following figures.

Now it's time for creating a design to consume the service.

Create an HTML page, here is the design,

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.930/styles/kendo.common.min.css">
  6. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.930/styles/kendo.rtl.min.css">
  7. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.930/styles/kendo.default.min.css">
  8. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.930/styles/kendo.mobile.all.min.css">
  9. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  10. <script src="http://kendo.cdn.telerik.com/2015.3.930/js/angular.min.js"></script>
  11. <script src="http://kendo.cdn.telerik.com/2015.3.930/js/jszip.min.js"></script>
  12. <script src="http://kendo.cdn.telerik.com/2015.3.930/js/kendo.all.min.js"></script>
  13. <meta charset="utf-8" />
  14. </head>
  15. <body>
  16. <div id="example">
  17. <div class="demo-section k-content wide">
  18. <div>
  19. <h4>Add an event</h4>
  20. <div data-role="scheduler"
  21. data-views="['day']"
  22. data-bind="source: tasks,
  23. visible: isVisible,
  24. style="height: 600px"></div>
  25. </div>
  26. <div style="padding-top: 1em;">
  27. <h4>Console</h4>
  28. <div class="console"></div>
  29. </div>
  30. </div>
  31. </body>
  32. </html>
JavaScript
  1. var viewModel = kendo.observable({
  2. isVisible: true,
  3. tasks: new kendo.data.SchedulerDataSource({
  4. batch: true,
  5. transport: {
  6. read: {
  7. url: "api/Events",
  8. dataType: "json"
  9. },
  10. parameterMap: function(options, operation) {
  11. if (operation !== "read" && options.models) {
  12. return {models: kendo.stringify(options.models)};
  13. }
  14. }
  15. },
  16. schema: {
  17. model: {
  18. id: "taskId",
  19. fields: {
  20. taskId: { from: "TaskID", type: "number" },
  21. title: { from: "Title", defaultValue: "No title", validation: { required: true } },
  22. start: { type: "date", from: "Start" },
  23. end: { type: "date", from: "EndDate" },
  24. startTimezone: { from: "StartTimezone" },
  25. endTimezone: { from: "EndTimezone" },
  26. description: { from: "Description" },
  27. recurrenceId: { from: "RecurrenceID" },
  28. recurrenceRule: { from: "RecurrenceRule" },
  29. recurrenceException: { from: "RecurrenceException" },
  30. isAllDay: { type: "boolean", from: "IsAllDay" }
  31. }
  32. }
  33. }
  34. })
  35. });
  36. kendo.bind($("#example"), viewModel);
The result in browser

Calander
Table
References

Conclusion:

From this article we learned how to remote bind the Kendo Scheduler using ASP.NET WEB PI and Entity Framework. In my upcoming article I am going to discuss about how to perform the CRUD operation in Kendo Scheduler and their events.