Hi Everyone!
Thanks for the recommendations for my previous post, Implementing Event Scheduling in Angular UI Calendar. In this tutorial, I am going to explain about implementing the CRUD operations on UI Calendar. If you are new to this post, please refer the above link to understand the implementation of a basic UI calendar and how to load the events data from server to display in calendar.
In this tutorial, we are using the code from the previous post. The below screenshot represents the previous application.

CRUD operations on Angular UI Calendar(Event Scheduling)
In this tutorial, we are using the code from the previous post. The below screenshot represents the previous application.

CRUD operations on Angular UI Calendar(Event Scheduling)
- Create a Visual Studio application (in this post, the application is created in VS 2015).
- See previous article to know the Calendar implementation; click here.
- You can find what libraries to add and how to get and display server data in UI-calendar from my previous post.
- I am using the same table schema and ADO.NET Data Model here.
Add Controller to display View and CRUD functions
- Right click on Controllers folder --> Add --> Select Empty Controller template --> name it (HomController) --> Add.
- Replace the previous code with the following code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using AngularUICalendarCRUd.Models;
- namespace AngularUICalendarCRUd.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- public JsonResult GetEvents()
- {
- //Here MyDatabaseEntities is our entity datacontext (see Step 4)
- using (DatabaseEntities dc = new DatabaseEntities())
- {
- var v = dc.Events.OrderBy(a => a.StartAt).ToList();
- return new JsonResult { Data = v, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
- }
- }
- //Action for Save event
- [HttpPost]
- public JsonResult SaveEvent(Event evt)
- {
- bool status = false;
- using (DatabaseEntities dc = new DatabaseEntities())
- {
- if (evt.EndAt != null && evt.StartAt.TimeOfDay == new TimeSpan(0, 0, 0) &&
- evt.EndAt.TimeOfDay == new TimeSpan(0, 0, 0))
- {
- evt.IsFullDay = true;
- }
- else
- {
- evt.IsFullDay = false;
- }
- if (evt.EventID > 0)
- {
- var v = dc.Events.Where(a => a.EventID.Equals(evt.EventID)).FirstOrDefault();
- if (v != null)
- {
- v.Title = evt.Title;
- v.Description = evt.Description;
- v.StartAt = evt.StartAt;
- v.EndAt = evt.EndAt;
- v.IsFullDay = evt.IsFullDay;
- }
- }
- else
- {
- dc.Events.Add(evt);
- }
- dc.SaveChanges();
- status = true;
- }
- return new JsonResult { Data = new { status = status } };
- }
- [HttpPost]
- public JsonResult DeleteEvent(int eventID)
- {
- bool status = false;
- using (DatabaseEntities dc = new DatabaseEntities())
- {
- var v = dc.Events.Where(a => a.EventID.Equals(eventID)).FirstOrDefault();
- if (v != null)
- {
- dc.Events.Remove(v);
- dc.SaveChanges();
- status = true;
- }
- }
- return new JsonResult { Data = new { status = status } };
- }
- }
- }
- SaveEvent saves the event data to database. DeleteEvent deletes the event data from the database.
- Index action is used to render the Index View.
- Make the below changes in _Layout.cshtml page, in Shared folder.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>@ViewBag.Title - My ASP.NET Application</title>
- <script src="~/Scripts/modernizr-2.6.2.js"></script>
- </head>
- <body>
- <div class="container body-content">
- @RenderBody()
- <hr />
- <footer>
- <p>© @DateTime.Now.Year - My ASP.NET Application</p>
- </footer>
- </div>
- </body>
- </html>
Add View to display the UI
- Right click on Index action in the HomeController.cs file --> Add --> name it (Index) --> Add.
- Replace the Index.cshtml page code with the below code.
- @{
- ViewBag.Title = "Index";
- }
- <h2>Angular UI calender Events Scheduling CRUD operations</h2>
- <!--Styles for UI calender-->
- <link href="~/Content/fullcalendar.css" rel="stylesheet" />
- <link href="~/Content/bootstrap.css" rel="stylesheet" />
- <!--Angualar script libraries-->
- <script src="~/Scripts/moment.js"></script>
- <script src="~/Scripts/jquery-1.11.3.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
- <script src="~/Scripts/calendar.js"></script>
- <script src="~/Scripts/fullcalendar.js"></script>
- <script src="~/Scripts/gcal.js"></script>
- <!--Current application Scripts-->
- <script src="~/Scripts/myscript.js"></script>
- <!--Bootstrap Angular UI Modal popup script library-->
- <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.3.2/ui-bootstrap-tpls.min.js"></script>
- @* HTML *@
- <div ng-app="myapp" ng-controller="CalenderController">
- <!--Angular template for modal dialog code structure starts here-->
- <script type="text/ng-template" id="modalContent.html">
- <div class="modal-header">
- <h3 class="modal-title">Events</h3>
- </div>
- <div class="modal-body">
- <div style="color:red">{{Message}}</div>
- <div class="form-group">
- <label>Event Title : </label>
- <input type="text" ng-model="NewEvent.Title" autofocus class="form-control" />
- </div>
- <div class="form-group">
- <label>Description : </label>
- <input type="text" ng-model="NewEvent.Description" class="form-control" />
- </div>
- <div class="form-group">
- <label>Time Slot : </label>
- <span>{{NewEvent.StartAt}} - {{NewEvent.EndAt}}</span>
- </div>
- </div>
- <div class="modal-footer">
- <button class="btn btn-primary" type="button" ng-click="ok()">Save</button>
- <button class="btn btn-danger" type="button" ng-show="NewEvent.EventID > 0" ng-click="delete()">Delete</button>
- <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
- </div>
- <!--Angular modal dialog code ends here-->
- </script>
- <div class="row">
- <div class="col-md-12">
- <!--this element displays the UI-caledar-->
- <div id="calendar" ui-calendar="uiConfig.calendar" ng-model="eventSources" calendar="myCalendar"></div>
- </div>
- </div>
- </div>
- I am using Bootstrap Angular UI Modal popup to Create, Edit, and Delete the events created in the UI calendar.




Cabinet DentistPosted May 2, 2017, 5:50 AM
Mine doesn't refresh calendar after update (add, edit or delete). I have the data in database, it shows when I refresh it with F5. Can you please help me out?
Ravi KandelPosted Aug 14, 2016, 3:17 AM
Awesome
Anu VPosted Aug 2, 2016, 12:26 AM
Nice
kalu singh raoPosted Aug 1, 2016, 1:49 AM
Good one
Vignesh ManiPosted Jul 30, 2016, 8:10 PM
Nice