License

Licensed for testing and evaluation purposes. This project uses a pre-release version of DayPilot Pro for ASP.NET Core. Please contact sales@daypilot.org to purchase a license.

Create a New ASP.NET Core Web Application Project

Create a new Visual Studio 2015 project using File -> New -> Project... menu option.

daypilot-pro-asp.net-core-new-project.png

Choose "Web Application" ASP.NET Core project template:

daypilot-pro-asp.net-core-project-type.png

Install DayPilot Pro for ASP.NET Core from .nupkg File

This pre-release version of DayPilot Pro for ASP.NET Core is not available through the public NuGet repository. You need to download DayPilot.Pro.AspNetCore.8.3.4.nupkg and install it from a local NuGet package source.

  1. Download DayPilot.Pro.AspNetCore.8.3.4.nupkg
  2. Save it to C:\DayPilotNuget\DayPilot.Pro.AspNetCore.8.3.4.nupkg

Right-click the project and open "Manage NuGet Packages...".

daypilot-pro-asp.net-core-manage-nuget.png

Click the gear icon to manage NuGet package sources.

daypilot-pro-asp.net-core-package-source.png

Add a new package source using "C:\DayPilotNuGet" path, click Update and OK:

daypilot-pro-asp.net-core-add-package-source-local.png

Select "DayPilot Local" as the package source and open the "Browse" tab in the NuGet Package Manager:

daypilot-pro-asp.net-core-nuget-install.png

Install DayPilot.Pro.AspNetCore package.

ASP.NET Core Scheduler Application

daypilot-pro-asp.net-core-scheduler.png

Open Views/Home/Index.cshtml and add <daypilot-scheduler> tag.

There are two required properties:

  • id - specifies the Scheduler ID, it's used for the placeholder <div> id attribute and also for the client-side DayPilot.Scheduler object that represents the Scheduler.
  • backend-url - specifies the backend controller URL

Index.cshtml (View)

@using DayPilot.AspNetCore;

@{
    ViewData["Title"] = "Home Page";
}

@addTagHelper "*, DayPilot.AspNetCore"

<daypilot-scheduler id="dp"
                    backend-url="/Home/Backend"
                    resources='new ResourceCollection() {
        new Resource("Room A", "A"),
        new Resource("Room B", "B"),
        new Resource("Room C", "C"),
        new Resource("Room D", "D"),
        new Resource("Room E", "E"),
        new Resource("Room F", "F")
    }' />

Now we need to add the backend controller action that will handle the internal communication with the client-side Scheduler object.

Add a new Backend() method to HomeController:

public IActionResult Backend()
{
  return new Dps().CallBack(this);
}

It passes control to Dps class which handles the server-side events. The first version of Dps class only handles OnInit event that initializes the Scheduler:

    public class Dps : DayPilotScheduler
    {
        protected override void OnInit(InitArgs e)
        {
            Update(CallBackUpdateType.Full);
        }
    }

HomeController.cs (Controller)

using DayPilot.AspNetCore;
using DayPilot.AspNetCore.Enums;
using DayPilot.AspNetCore.Events.Scheduler;
using Microsoft.AspNetCore.Mvc;

namespace TutorialDayPilotAspNetCorePreview.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Backend()
        {
            return new Dps().CallBack(this);
        }
    }

    public class Dps : DayPilotScheduler
    {
        protected override void OnInit(InitArgs e)
        {
            Update(CallBackUpdateType.Full);
        }
    }
}

Loading Resources

daypilot-pro-asp.net-core-scheduler-resources.png

It's possible to specify the resources using the <daypilot-scheduler> tag in the view as we did in the first example:

@using DayPilot.AspNetCore;

@{
    ViewData["Title"] = "Home Page";
}

@addTagHelper "*, DayPilot.AspNetCore"

<daypilot-scheduler id="dp"
                    backend-url="/Home/Backend"
                    resources='new ResourceCollection() {
        new Resource("Room A", "A"),
        new Resource("Room B", "B"),
        new Resource("Room C", "C"),
        new Resource("Room D", "D"),
        new Resource("Room E", "E"),
        new Resource("Room F", "F")
    }' />

Normally you don't want to use a static list. It's more common to load resources from the database. There are two options:

  1. You can pass the resource list to the <daypilot-scheduler> config as a MVC model.
  2. You can load it using the OnInit event in the Dps class.

The OnInit event is fired immediately after the Scheduler is initialized on the client-side.

public class Dps : DayPilotScheduler
{
    protected override void OnInit(InitArgs e)
    {

        Resources = new ResourceCollection()
        {
            new Resource("Room A", "A"),
            new Resource("Room B", "B"),
            new Resource("Room C", "C"),
            new Resource("Room D", "D"),
            new Resource("Room E", "E"),
            new Resource("Room F", "F")
        };

        Update(CallBackUpdateType.Full);
    }
}

Loading Events

daypilot-pro-asp.net-core-scheduler-events.png

We will also use OnInit event to load the events. The "Events" property accepts an IEnumerable object that holds arbitrary objects. This way you can reuse objects returned from database. You just need to map the key fields/properties:

  • DataIdField - specifies the primary key
  • DataStartField - specifies the field with events start (must be convertible to DateTime)
  • DataEndField - specifies the field with events start (must be convertible to DateTime)
  • DataResourceField - specifies the field with resource id (used to match events and resources/rows)
  • DataTextField - specifies the field with event name/text

This example uses "EventData" class which is an ad-hoc structure that holds the event data.

public class Dps : DayPilotScheduler
{
    protected override void OnInit(InitArgs e)
    {
    
        // ...

        Events = new List<EventData>
        {
            new EventData() { Id = "1", Resource = "A", Start = new DateTime(2016, 12, 10), End = new DateTime(2016, 12, 15), Text = "Event 1"},
            new EventData() { Id = "2", Resource = "B", Start = new DateTime(2016, 12, 10), End = new DateTime(2016, 12, 15), Text = "Event 2"},
        };

        DataIdField = "Id";
        DataStartField = "Start";
        DataEndField = "End";
        DataResourceField = "Resource";
        DataTextField = "Text";

        Update(CallBackUpdateType.Full);
    }
}

public class EventData
{
    public string Id;
    public string Resource;
    public DateTime Start;
    public DateTime End;
    public string Text;
}

Scale and Time Headers

With the default configuration, the Scheduler timeline starts at 12am (midnight) today and it displays one day with one hour as the grid time unit:

daypilot-pro-asp.net-core-scheduler-default-timeline.png

We want to customize the timeline so it displays 31 days of December 2016 (StartDate and Days properties):

public class Dps : DayPilotScheduler
{
protected override void OnInit(InitArgs e)
{
// ...
StartDate = new DateTime(2016, 12, 1);
Days = 31;
// ...
}
}

with the grid unit set to one day (Scale property):

public class Dps : DayPilotScheduler
{
protected override void OnInit(InitArgs e)
{
// ...
StartDate = new DateTime(2016, 12, 1);
Days = 31;
Scale = TimeScale.Day;
// ...
}
}

And we will also adjust the time headers accordingly. The time header now displays months and days in two rows (TimeHeaders property).

This is how the scheduler looks with the new configuration:

daypilot-pro-asp.net-core-scheduler-time-headers.png

public class Dps : DayPilotScheduler
{
protected override void OnInit(InitArgs e)
{
// ...
StartDate = new DateTime(2016, 12, 1);
Days = 31;
Scale = TimeScale.Day;
TimeHeaders = new TimeHeaderCollection()
{
new TimeHeader(GroupBy.Month),
new TimeHeader(GroupBy.Day)
};
Update(CallBackUpdateType.Full);
}
}