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
Create table
- USE [TimeLineDB]
- GO
-
- /****** Object: Table [dbo].[TimeLine_tbt] Script Date: 9/16/2016 10:03:21 AM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[TimeLine_tbt](
- [id] [int] NOT NULL,
- [content] [varchar](50) NULL,
- [start] [datetime] NULL,
- CONSTRAINT [PK_TimeLine_tbt] PRIMARY KEY CLUSTERED
- (
- [id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- 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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace TimeLineApp.Controllers
- {
- public class TimelineController : Controller
- {
-
- private TimeLineDBEntities1 context = new TimeLineDBEntities1();
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- public JsonResult GetEventsList()
- {
- var data = context.TimeLine_tbt.ToList();
-
- return Json(data, JsonRequestBehavior.AllowGet);
- }
-
- }
- }
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.
- <!-- CSS -->
- t;script src="~/Scripts/vis.min.js"></script>
- <!-- JS -->
- t;link href="~/Content/vis.min.css" rel="stylesheet" />
Index cshtml
- @{
- ViewBag.Title = "Timeline demo";
- }
-
- <h2>Timeline | ASP.NET MVC 5</h2>
-
- @section scripts{
-
- <!-- CSS -->
- <script src="~/Scripts/vis.min.js"></script>
- <!-- JS -->
- <link href="~/Content/vis.min.css" rel="stylesheet" />
-
- <script type="text/javascript">
-
- $(document).ready(function(){
-
-
- $.ajax({
- type: "GET",
- url: "GetEventsList",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: ChartVis,
- error: OnError
-
- });
-
-
- function ChartVis(response)
- {
-
- var container = document.getElementById('visualization');
-
-
- var items = new vis.DataSet(response);
-
-
- var options = {};
-
-
- var timeline = new vis.Timeline(container, items, options);
- }
- function OnError(response) {
- alert("Error !");
- }
-
-
- })
-
- </script>
-
- }
-
-
- <div id="visualization"></div>
Output