To-Do List Implementation With ASP.NET Core, TypeScript And Gijgo

In this article, you will learn about To-Do list implementation with ASP.NET Core, TypeScript And Gijgo.

 
 
Introduction

This article is going to show how you can easily create TODO List with Gijgo Controls and TypeScript in ASP.NET Core.

Background

In this article, I'm going to use jQuery Grid and jQuery Dialog 0.6.2 by gijgo.com, jQuery 2.2.3, TypeScript 1.8, Entity Framework Core 1.0.0 and ASP.NET Core MVC 1.0.0.

A few words about the libraries that I'm going use in this article:

  • ASP.NET Core is a re-implementation of ASP.NET as a modular Web framework, together with other frameworks like Entity Framework.The new framework uses the new open-source .NET Compiler Platform (codename "Roslyn"),  a cross platform.
  • Gijgo is a set of free open source JavaScript controls, distributed under MIT License. All the widgets are of high performance and built on top of the jQuery JavaScript Library. Gijgo is compatible with jQuery UI, Bootstrap and Foundation.
  • TypeScript is a free and an open source programming language, developed and maintained by Microsoft. It is a strict superset of JavaScript, adds optional static typing and a class-based object-oriented programming to the language. 
Pre-Requirements

In order to be able to run the example from the attachment or build it from scratch, you need to have the following tools:

How to run the example from the attachment.

If you want to run the example from the attachment, you have to follow the steps, given below:

  1. Open the project in Visual Studio 2015.
  2. Execute "Add-Migration InitialCreate" and then "Update-Database" in "Package Manager Console" in order to create the database.

    add-update-db

  3. Run the project and perform CRUD operations with players.
How to build it from scratch

If you want to build the same app from scratch, you can follow the steps below. Keep in mind, that this can change in the future, because I'm using release candidates that may change.

  1. Open Visual Studio 2015 and create new Web project from type "ASP.NET Core Web Application (.NET Core)".



  2. Choose "Empty" template for your Application.



  3. Run the following commands in "Package Manager Console" to install ASP.NET MVC Core and Entity Framework Core.

    • Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 1.0.0
    • Install-Package Microsoft.EntityFrameworkCore.Tools -Version 1.0.0-preview2-final
    • Install-Package Microsoft.EntityFrameworkCore.Tools.Core -Version 1.0.0-rc2-final
    • Install-Package Microsoft.AspNetCore.Mvc -Version 1.0.0
    • Install-Package Microsoft.AspNetCore.Razor.Tools -Version 1.0.0-preview2-final
    • Install-Package Microsoft.AspNetCore.StaticFiles -Version 1.0.0
    • Install-Package Microsoft.Extensions.Configuration.Json -Version 1.0.0
    • Install-Package Microsoft.Extensions.Configuration.EnvironmentVariables -Version 1.0.0

  4. Add new item to the project from type "Client-side/Bower Configuration File" (bower.json).



  5. Add dependencies in bower.json to jquery 2.2.3, bootstrap 3.3.6 and gijgo 0.6.2.This is going to automatically include those 2 libraries in the "wwwroot/libs" folder of your project.



  6. Open "Node.js command prompt" and navigate to the wwwroot folder in your project.

  7. Run the following commands in the command prompt:

    • npm install tsd@next -g (install TypeScript Definition manager for DefinitelyTyped)
    • tsd install jQuery --save (install TypeScript definitions for jquery)
    • tsd install gijgo --save (install TypeScript definitions for gijgo)



  8. Add "ASP.NET Configuration File" to your solution called appsettings.json.



  9. Configure correct default connection string to DB that you are going to use for this example. Don't create the database yet, because the database will be created automatically by the EntityFramework.



  10. Create Models folder at a root level of the project.

  11. Add TODOItem class, that represents the TODO entity to the Models folder.
    1. public class TODOItem  
    2. {  
    3.      public int ID { getset; }  
    4.      public string Name { getset; }  
    5.      public bool IsCompleted { getset; }  
    6. }  
  12. Add ApplicationDbContext class to the Models folder.
    1. public class ApplicationDbContext : DbContext  
    2. {  
    3.      public DbSet TODOItems { getset; }  

    4.      public ApplicationDbContext(DbContextOptions options)  
    5.      : base(options)  
    6.      {  
    7.      }

    8.      protected override void OnModelCreating(ModelBuilder builder)  
    9.      {  
    10.         base.OnModelCreating(builder);  
    11.      }
    12. }  
  13. Enable appsettings.json, EntityFramework and MVC in the StartUp.cs file, as it shown below:
    1. public class Startup  
    2. {  
    3.      public IConfigurationRoot Configuration { getset; }  
    4.      public Startup(IHostingEnvironment env)  
    5.      {  
    6.         var builder = new ConfigurationBuilder()  
    7.            .SetBasePath(env.ContentRootPath)  
    8.            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)  
    9.            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);  
    10.         builder.AddEnvironmentVariables();  
    11.         Configuration = builder.Build();  
    12.      }  
    13.      public void ConfigureServices(IServiceCollection services)  
    14.      {  
    15.         services.AddDbContext(options =>  
    16.            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));  
    17.         services.AddMvc();  
    18.      }  
    19.      public void Configure(IApplicationBuilder app)  
    20.      {  
    21.         app.UseStaticFiles();  
    22.         app.UseMvc(routes => {  
    23.            routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");  
    24.         });  
    25.      }  
    26. }  
  14. Add Controllers folder and HomeController.cs to manage operations with TODO items on the Server side inside this folder.
    1. public class HomeController : Controller  
    2. {  
    3.      private ApplicationDbContext _context;  

    4.      public HomeController(ApplicationDbContext context)  
    5.      {  
    6.         this._context = context;  
    7.      }  

    8.      // GET: //  
    9.      public IActionResult Index()  
    10.      {  
    11.         return View();  
    12.      }  

    13.      public JsonResult GetTODOItems(int? page, int? limit, string searchString = null)  
    14.      {  
    15.         var records = this._context.TODOItems.AsQueryable();  
    16.         if (!string.IsNullOrWhiteSpace(searchString))  
    17.         {  
    18.            records = records.Where(p => p.Name.Contains(searchString));  
    19.         }  
    20.         int total = records.Count();  
    21.         if (page.HasValue && limit.HasValue)  
    22.         {  
    23.            int start = (page.Value - 1) * limit.Value;  
    24.            records = records.Skip(start).Take(limit.Value);  
    25.         }  
    26.         return Json(new { records, total }, new JsonSerializerSettings  
    27.         {  
    28.            ContractResolver = new DefaultContractResolver { NamingStrategy = null }  
    29.         });  
    30.      } 

    31.      [HttpPost]  
    32.      public JsonResult Save(TODOItem todoItem)  
    33.      {  
    34.         if (todoItem.ID > 0)  
    35.         {  
    36.            var entity = this._context.TODOItems.First(p => p.ID == todoItem.ID);  
    37.            if (!string.IsNullOrWhiteSpace(todoItem.Name))  
    38.            {  
    39.               entity.Name = todoItem.Name;  
    40.            }  
    41.            if (todoItem.IsCompleted != null)  
    42.            {  
    43.               entity.IsCompleted = todoItem.IsCompleted;  
    44.            }  
    45.         }  
    46.         else  
    47.         {  
    48.            this._context.TODOItems.Add(new TODOItem  
    49.            {  
    50.               Name = todoItem.Name,  
    51.               IsCompleted = false  
    52.            });  
    53.         }  
    54.         this._context.SaveChanges();  
    55.         return Json(true);  
    56.      }  

    57.      [HttpPost]  
    58.      public JsonResult Remove(int id)  
    59.      {  
    60.         var entity = this._context.TODOItems.First(p => p.ID == id);  
    61.         this._context.TODOItems.Remove(entity);  
    62.         this._context.SaveChanges();  
    63.         return Json(true);  
    64.      }  
    65. }  
  15. Create Views folder and Home folder with Index.cshtml inside the Views folder. Add the HTML structure in the Index.cshtml, which is required for the management of TODO items. You can find a sample code about it in the $Views\Home\Index.cshtml file from the attachment.

  16. Create /wwwroot/scripts/site.ts file that manages TODO items with TypeScript on the client side. You can find a sample code about it in the $wwwroot\scripts\site.ts file from the attachment.

  17. Add manually Microsoft.EntityFrameworkCore.Tools to the tools section in the project.json file.



  18. Execute "Add-Migration InitialCreate" and then "Update-Database" in "Package Manager Console" in order to create the database.



  19. Run the project and manage your TODO Items.

I hope that this article is going to be useful for your project.