Using FullCalendar jQuery component with ASP.NET MVC

I found very good jQuery component called FullCalendar. My special favorites are agenda views because they make this calendar really useful in business applications. In this posting I will show you how to use FullCalendar with ASP.NET MVC.

SOURCE CODE Source code of this project is available at CodePlex. Please visit MVC Time Planner page for further information and downloads.

Prerequisites

jQuery FullCalendar filesYou need the following JavaScript components in your MVC project:

Download these components and include them to your ASP.NET MVC project. If you have problems then take a look at the image on right – you can what files I included and where they are located. Click on the image to see it at full size.

I included jQueryUI styles for one good reason – I am not designer but ol’ school MS Paint gangsta and I will be never able to make such a nice designs as pros. I think I am not the only one. :)

Including scripts and styles

As a next thing let’s include script and styles we need to make calendar work. Because my sample application offers currently no more functionality I included these scripts to my master page.

<link href="<%= Url.Content("~/Content/jquery-ui-1.7.2.custom.css") %>" rel="stylesheet" type="text/css" />
<
link href="<%= Url.Content("~/Content/fullcalendar.css") %>" rel="stylesheet" type="text/css" />
 
<script type="text/javascript" 
src="<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>
">
</
script
>
<
script type="text/javascript" 
src="<%= Url.Content("~/Scripts/jquery-ui-1.7.2.custom.min.js") %>
">
</
script
>
<
script type="text/javascript" 
src="<%= Url.Content("~/Scripts/fullcalendar.min.js") %>
">
</
script
>

Url.Content method creates correct URL-s and it is way better than MS strategy (href like ..\..\Content\Site.css).

Defining FullCalendar

FullCalendar is typical jQuery component. It needs a container with unique id to be defined in HTML. By example:

<div id="calendar"></div>

Now let’s use jQuery to make calendar look like calendar. Now paste the following script to page header between <script> and </script> tags.

$(document).ready(function () {
    $(
'#calendar'
).fullCalendar({
        theme:
true
,
        header: {
            left:
''
,
            center:
''
,
            right:
''
        },
        defaultView:
'agendaDay'
,
        editable:
false
,
        events:
"/Calendar/GetEvents/"
    });
});

Now you should see calendar like this when you run your application and move to the page where you added FullCalendar.

FullCalendar day agenda view

Reading events from server

If you look at calendar definition of calendar you can see that there is attribute called events (currently it has value "Calendar/GetEvents/"). FullCalendar uses this URL to get data about event sin selected view. Let’s take a look request to this URL in FireBug.

FullCalendar event info request

We can see that this request contains three parts: start and end time of current view and one additional parameter that avoids browsers caching the response. Also we can see that start and end times are given as timestamps from Epoch.

Let’s see the controller action that returns events for us.

public JsonResult GetEvents(double start, double end)
{
   
var userName = Session["UserName"] as string
;
   
if (string
.IsNullOrEmpty(userName))
    {
       
return null
;
    }

   
var
fromDate = ConvertFromUnixTimestamp(start);
   
var
toDate = ConvertFromUnixTimestamp(end);

   
var rep = Resolver.Resolve<IEventRepository
>();
   
var
events = rep.ListEventsForUser(userName, fromDate, toDate);

   
var eventList = from e in
events
                   
select new
                    {
                        id = e.Id,
                        title = e.Title,
                        start = e.FromDate.ToString(
"s"
),
                        end = e.ToDate.ToString(
"s"
),
                        allDay =
false
                    };

   
var
rows = eventList.ToArray();
   
return Json(rows, JsonRequestBehavior.AllowGet);
}

ConvertFromUnixTimestamp method is borrowed from CodeClimber blog posting Convert a Unix timestamp to a .NET DateTime. So, let’s say thanks to Simone Chiaretta.

private static DateTime ConvertFromUnixTimestamp(double timestamp)
{
   
var origin = new
DateTime(1970, 1, 1, 0, 0, 0, 0);
   
return origin.AddSeconds(timestamp);
}

If there were no problems and we inserted some events to database then we should see something like this. Pretty nice?

FullCalendar day agenda with events

As a quick update you can follow my previous posting about FullCalendar Linking jQueryUI DatePicker and FullCalendar and link you calendar to jQueryUI DatePicker. The result is here.

FullCalendar with date selector

That’s it for now. Happy coding! :)

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    44 thoughts on “Using FullCalendar jQuery component with ASP.NET MVC

    • February 4, 2010 at 12:28 pm
      Permalink

      Hi,
      can you download your copy ready? I do not get the point :(.

    • February 4, 2010 at 3:05 pm
      Permalink

      Hi Timo!

      What problems you are facing?

    • February 5, 2010 at 3:23 pm
      Permalink

      Hi,
      I skipped the MVC :-). How can I build without MVC?

      Greeting
      Timo

    • February 5, 2010 at 8:04 pm
      Permalink

      Can this be setup to connect to/pull from Outlook ?

    • February 5, 2010 at 9:46 pm
      Permalink

      Steve, it is usual web application and it is not able to communicate with Outlook that runs on your desktop. You can extend it to add support for vCal or iCal downloads. Outlook can detect those files and add events to your calendar.

    • February 8, 2010 at 1:02 pm
      Permalink

      I don’t have ASP.NET forms example. I have ASP.NET MVC version only. If it is okay for you I can send it to you. If you want forms example then you must wait some days as I am pretty busy right now. Please send me a message with your e-mail address and I will send you the example like it is right now.

    • February 8, 2010 at 2:12 pm
      Permalink

      Hi,
      ok thanks. I sent you an e-mail!

      Thank you very much

    • February 15, 2010 at 8:17 pm
      Permalink

      I too would be interested in seeing this sample code if possible…dave -at- thehorners.com. Thanks!!!

    • February 25, 2010 at 9:04 pm
      Permalink

      Can you please send me an example for asp.net via email too at yhrchan@hotmail.com?

      Thanks,
      Ricky

    • March 26, 2010 at 10:01 pm
      Permalink

      Awesome post – you saved me a lot of time. Thanks a lot!!!

    • March 27, 2010 at 7:07 am
      Permalink

      Please send the code… thanks

    • May 26, 2010 at 4:52 pm
      Permalink

      Can you send me the sample code please? My email is: spencerclark -at- gmail.com

      Thanks for producing such great code!

    • June 21, 2010 at 12:41 am
      Permalink

      I would also like a copy of the code if possible.
      james.west at westdarley . co . uk
      Thank you

    • June 28, 2010 at 9:14 am
      Permalink

      very cool code and i will get you sourc code from codeplex

      Thank so much.

    • February 5, 2010 at 9:45 pm
      Permalink

      Timo, you can use basically same code also for ASP.NET Forms. You just have to create JSON responses and write them out. After that you should immediately close response to avoid another mark-up. Also you should set correct content type. It is all pretty simple.

    • November 20, 2010 at 10:19 am
      Permalink

      Raman, you should be able to control HTML of calendar. You can use divs in calendar and switch them on and off in JavaScript.

    • April 15, 2011 at 7:35 am
      Permalink

      do you have any story about how to integrate with wdCalendar?

    • April 16, 2011 at 10:23 am
      Permalink

      Hey.
      I guess I’m the only one having this problem but this technique doesn’t show any events for me.
      I think it’s because DateTime.ToString(“s”) gives a string of the format:”2011-04-10T17:00:00″… The T seems to be the problem… Anyone got a solution for this?

    • April 16, 2011 at 11:24 am
      Permalink

      nevermind…

    • April 16, 2011 at 11:25 am
      Permalink

      great tut by the way. thanx

    • April 16, 2011 at 11:49 am
      Permalink

      Is it possible to synchronize my fullcalendar with a google calendar? What about downloading the calendar as an excel file, pdf or word?

    • April 16, 2011 at 7:00 pm
      Permalink

      Thanks for feedback, nchekwube!

      I have tons of plans how to improve this calendar and I hope to continue with it as soon as some of my busy projects are not so busy anymore. :)

    • April 21, 2011 at 7:58 pm
      Permalink

      Where is IEventRepository defined?

    • April 22, 2011 at 9:32 am
      Permalink

      IEventRepository is my own interface defined my own code.

    • May 4, 2011 at 6:42 am
      Permalink

      where is Json defined?

    • May 6, 2011 at 6:38 pm
      Permalink

      Very cool, extremely useful and simple to implement. Thank you.

    • May 16, 2011 at 12:20 pm
      Permalink

      I want to customize the JSON call for event.
      How can i add parameters to the following function public JsonResult GetEvents(double start, double end)

      I have to add some more filter parameters. Currently i am doing it by session variables. But i do not want to use session for this

    • June 24, 2011 at 6:12 pm
      Permalink

      Were you able to update the control to month/week views? just curious.

    • July 10, 2011 at 7:11 am
      Permalink

      Can this script be used with ASP.NET MVC3 RAZOR..?? Please help.
      If not, can you give any calender script that can work with ASP.NET MVC3 RAZOR.

    • August 26, 2011 at 3:27 pm
      Permalink

      thnk u for the tutoriel!

      i have a problem with this line

      Resolver.Resolve();

      my project doesn’t know this class (Resolver) , same think with the interface (IEventRepository)

      should i import an additionnal assembly ?

    • March 13, 2012 at 12:17 pm
      Permalink

      can i make it read events from a database i made it ??? plz i need answer

    • August 22, 2012 at 1:25 pm
      Permalink

      this’s really good :)
      thx bro ;)

    • Pingback:ASP.NET MVC Time Planner is available at CodePlex | Gunnar Peipman - Programming Blog

    • February 2, 2015 at 3:57 pm
      Permalink

      Thanks so much for the tutorial!

    • August 25, 2016 at 3:44 pm
      Permalink

      GetEvents(double start, double end)
      start and end getting null. am i missing anything?

    Leave a Reply

    Your email address will not be published. Required fields are marked *