Introduction

In this post, I will show you, how to create a timeline, based on vis.js plugin, using ASP.NET MVC5 and Entity Framework.

Prerequisites

As I said before, we are going to use timeline plugin in our MVC Application. For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.

SQL database part

Here, you find the scripts to create the database and table.

Create database

  1. USE [master]
  2. GO
  3. /****** Object: Database [TimeLineDB] Script Date: 9/16/2016 10:02:47 AM ******/
  4. CREATE DATABASE [TimeLineDB]
  5. CONTAINMENT = NONE
  6. ON PRIMARY
  7. ( NAME = N'TimeLineDB', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TimeLineDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
  8. LOG ON
  9. ( NAME = N'TimeLineDB_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TimeLineDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
  10. GO
  11. ALTER DATABASE [TimeLineDB] SET COMPATIBILITY_LEVEL = 110
  12. GO
  13. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
  14. begin
  15. EXEC [TimeLineDB].[dbo].[sp_fulltext_database] @action = 'enable'
  16. end
  17. GO
  18. ALTER DATABASE [TimeLineDB] SET ANSI_NULL_DEFAULT OFF
  19. GO
  20. ALTER DATABASE [TimeLineDB] SET ANSI_NULLS OFF
  21. GO
  22. ALTER DATABASE [TimeLineDB] SET ANSI_PADDING OFF
  23. GO
  24. ALTER DATABASE [TimeLineDB] SET ANSI_WARNINGS OFF
  25. GO
  26. ALTER DATABASE [TimeLineDB] SET ARITHABORT OFF
  27. GO
  28. ALTER DATABASE [TimeLineDB] SET AUTO_CLOSE OFF
  29. GO
  30. ALTER DATABASE [TimeLineDB] SET AUTO_CREATE_STATISTICS ON
  31. GO
  32. ALTER DATABASE [TimeLineDB] SET AUTO_SHRINK OFF
  33. GO
  34. ALTER DATABASE [TimeLineDB] SET AUTO_UPDATE_STATISTICS ON
  35. GO
  36. ALTER DATABASE [TimeLineDB] SET CURSOR_CLOSE_ON_COMMIT OFF
  37. GO
  38. ALTER DATABASE [TimeLineDB] SET CURSOR_DEFAULT GLOBAL
  39. GO
  40. ALTER DATABASE [TimeLineDB] SET CONCAT_NULL_YIELDS_NULL OFF
  41. GO
  42. ALTER DATABASE [TimeLineDB] SET NUMERIC_ROUNDABORT OFF
  43. GO
  44. ALTER DATABASE [TimeLineDB] SET QUOTED_IDENTIFIER OFF
  45. GO
  46. ALTER DATABASE [TimeLineDB] SET RECURSIVE_TRIGGERS OFF
  47. GO
  48. ALTER DATABASE [TimeLineDB] SET DISABLE_BROKER
  49. GO
  50. ALTER DATABASE [TimeLineDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
  51. GO
  52. ALTER DATABASE [TimeLineDB] SET DATE_CORRELATION_OPTIMIZATION OFF
  53. GO
  54. ALTER DATABASE [TimeLineDB] SET TRUSTWORTHY OFF
  55. GO
  56. ALTER DATABASE [TimeLineDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
  57. GO
  58. ALTER DATABASE [TimeLineDB] SET PARAMETERIZATION SIMPLE
  59. GO
  60. ALTER DATABASE [TimeLineDB] SET READ_COMMITTED_SNAPSHOT OFF
  61. GO
  62. ALTER DATABASE [TimeLineDB] SET HONOR_BROKER_PRIORITY OFF
  63. GO
  64. ALTER DATABASE [TimeLineDB] SET RECOVERY SIMPLE
  65. GO
  66. ALTER DATABASE [TimeLineDB] SET MULTI_USER
  67. GO
  68. ALTER DATABASE [TimeLineDB] SET PAGE_VERIFY CHECKSUM
  69. GO
  70. ALTER DATABASE [TimeLineDB] SET DB_CHAINING OFF
  71. GO
  72. ALTER DATABASE [TimeLineDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
  73. GO
  74. ALTER DATABASE [TimeLineDB] SET TARGET_RECOVERY_TIME = 0 SECONDS
  75. GO
  76. ALTER DATABASE [TimeLineDB] SET READ_WRITE
  77. GO
Create table
  1. USE [TimeLineDB]
  2. GO
  3. /****** Object: Table [dbo].[TimeLine_tbt] Script Date: 9/16/2016 10:03:21 AM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. CREATE TABLE [dbo].[TimeLine_tbt](
  11. [id] [int] NOT NULL,
  12. [content] [varchar](50) NULL,
  13. [start] [datetime] NULL,
  14. CONSTRAINT [PK_TimeLine_tbt] PRIMARY KEY CLUSTERED
  15. (
  16. [id] ASC
  17. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  18. ) ON [PRIMARY]
  19. GO SET ANSI_PADDING OFF
    GO
After creating the table, you can add some records, as shown below for demo-




Create your MVC Application

First of all, open Visual Studio and select file, click new project and a new dialog will pop up with the name New Project, select ASP.NET Web Application (.NET Framework), name your project and click OK button.



Now, new dialog will pop up to select the template. We are going to choose MVC template and click OK button.



After creating our project, we are going to add ADO.NET Entity Data Model.

Adding ADO.NET Entity Data Model

For this, right click on the project name, click Add > Add New Item. A dialog box will pop up. Inside Visual C#, select data, followed by ADO.NET Entity Data Model, enter the name for your Dbcontext model as DbContextTimeline and finally click Add.



At this stage, we are going to choose EF Designer from database, as given below-



In the snapshot given below, we need to select your Server name, then via dropdown list in connect to a database section, you should choose your database name and finally click OK button.





After clicking Next button, the dialog Entity Data Model wizard will pop up to choose object, which we want to use. In this example, we are going to chose TimeLine_tbt table and click Finish button. Finally, we see EDMX model generates TimeLine_tbt class.



Create a controller

Now, we are going to create a controller. Right click on the controllers folder > Add > Controller> selecting MVC 5 Controller – Empty > click Add.



Enter Controller name (‘TimelineController’).



TimelineController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace TimeLineApp.Controllers
  7. {
  8. public class TimelineController : Controller
  9. {
  10. //DbContext
  11. private TimeLineDBEntities1 context = new TimeLineDBEntities1();
  12. // GET: Timeline
  13. public ActionResult Index()
  14. {
  15. return View();
  16. }
  17. public JsonResult GetEventsList()
  18. {
  19. var data = context.TimeLine_tbt.ToList();
  20. return Json(data, JsonRequestBehavior.AllowGet);
  21. }
  22. }
  23. }
Here, I’m creating GetEventsList() action, which will select all the data from TimeLine_tbt table and return result as JSON.

Adding View

In timeline controller, just right click on Index() action, select Add View and dialog will pop up, write a name for your view, finally click Add.



Note- Don’t forget to download the following libraries from vis.js.
  1. <!-- CSS -->
  2. t;script src="~/Scripts/vis.min.js"></script>
  3. <!-- JS -->
  4. t;link href="~/Content/vis.min.css" rel="stylesheet" />
Index cshtml
  1. @{
  2. ViewBag.Title = "Timeline demo";
  3. }
  4. <h2>Timeline | ASP.NET MVC 5</h2>
  5. @section scripts{
  6. <!-- CSS -->
  7. <script src="~/Scripts/vis.min.js"></script>
  8. <!-- JS -->
  9. <link href="~/Content/vis.min.css" rel="stylesheet" />
  10. <script type="text/javascript">
  11. $(document).ready(function(){
  12. $.ajax({
  13. type: "GET",
  14. url: "GetEventsList",
  15. contentType: "application/json; charset=utf-8",
  16. dataType: "json",
  17. success: ChartVis,
  18. error: OnError
  19. });
  20. function ChartVis(response)
  21. {
  22. // DOM element where the Timeline will be attached
  23. var container = document.getElementById('visualization');
  24. // Create a DataSet (allows two way data-binding)
  25. var items = new vis.DataSet(response);
  26. // Configuration for the Timeline
  27. var options = {};
  28. // Create a Timeline
  29. var timeline = new vis.Timeline(container, items, options);
  30. }
  31. function OnError(response) {
  32. alert("Error !");
  33. }
  34. })
  35. </script>
  36. }
  37. <div id="visualization"></div>
Output