Master Details Using Web API 2 And KnockOut.JS

Introduction

In this post, I will show you how to create master details, which are based on knockout.js library, using ASP.NET Web API 2 and Entity Framework.

Prerequisites

As I said before, to achieve our requirement, we must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.

In this article, we are going to:

  • Create a database.
  • Create Web API Application.
  • Configuring Entity Framework ORM.
  • Implementing http Services, which should be used.
  • Using knockout.js library for calling Services.

SQL database part

Here, you will find the scripts to create database and tables.

Create database 

  1. USE [master]  
  2. GO  
  3.   
  4. /****** Object:  Database [CompanyDB]    Script Date: 3/11/2017 8:18:51 AM ******/  
  5. CREATE DATABASE [CompanyDB]  
  6.  CONTAINMENT = NONE  
  7.  ON  PRIMARY   
  8. NAME = N'CompanyDB', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CompanyDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  9.  LOG ON   
  10. NAME = N'CompanyDB_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CompanyDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  11. GO  
  12.   
  13. ALTER DATABASE [CompanyDB] SET COMPATIBILITY_LEVEL = 110  
  14. GO  
  15.   
  16. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  17. begin  
  18. EXEC [CompanyDB].[dbo].[sp_fulltext_database] @action = 'enable'  
  19. end  
  20. GO  
  21.   
  22. ALTER DATABASE [CompanyDB] SET ANSI_NULL_DEFAULT OFF   
  23. GO  
  24.   
  25. ALTER DATABASE [CompanyDB] SET ANSI_NULLS OFF   
  26. GO  
  27.   
  28. ALTER DATABASE [CompanyDB] SET ANSI_PADDING OFF   
  29. GO  
  30.   
  31. ALTER DATABASE [CompanyDB] SET ANSI_WARNINGS OFF   
  32. GO  
  33.   
  34. ALTER DATABASE [CompanyDB] SET ARITHABORT OFF   
  35. GO  
  36.   
  37. ALTER DATABASE [CompanyDB] SET AUTO_CLOSE OFF   
  38. GO  
  39.   
  40. ALTER DATABASE [CompanyDB] SET AUTO_CREATE_STATISTICS ON   
  41. GO  
  42.   
  43. ALTER DATABASE [CompanyDB] SET AUTO_SHRINK OFF   
  44. GO  
  45.   
  46. ALTER DATABASE [CompanyDB] SET AUTO_UPDATE_STATISTICS ON   
  47. GO  
  48.   
  49. ALTER DATABASE [CompanyDB] SET CURSOR_CLOSE_ON_COMMIT OFF   
  50. GO  
  51.   
  52. ALTER DATABASE [CompanyDB] SET CURSOR_DEFAULT  GLOBAL   
  53. GO  
  54.   
  55. ALTER DATABASE [CompanyDB] SET CONCAT_NULL_YIELDS_NULL OFF   
  56. GO  
  57.   
  58. ALTER DATABASE [CompanyDB] SET NUMERIC_ROUNDABORT OFF   
  59. GO  
  60.   
  61. ALTER DATABASE [CompanyDB] SET QUOTED_IDENTIFIER OFF   
  62. GO  
  63.   
  64. ALTER DATABASE [CompanyDB] SET RECURSIVE_TRIGGERS OFF   
  65. GO  
  66.   
  67. ALTER DATABASE [CompanyDB] SET  DISABLE_BROKER   
  68. GO  
  69.   
  70. ALTER DATABASE [CompanyDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  71. GO  
  72.   
  73. ALTER DATABASE [CompanyDB] SET DATE_CORRELATION_OPTIMIZATION OFF   
  74. GO  
  75.   
  76. ALTER DATABASE [CompanyDB] SET TRUSTWORTHY OFF   
  77. GO  
  78.   
  79. ALTER DATABASE [CompanyDB] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  80. GO  
  81.   
  82. ALTER DATABASE [CompanyDB] SET PARAMETERIZATION SIMPLE   
  83. GO  
  84.   
  85. ALTER DATABASE [CompanyDB] SET READ_COMMITTED_SNAPSHOT OFF   
  86. GO  
  87.   
  88. ALTER DATABASE [CompanyDB] SET HONOR_BROKER_PRIORITY OFF   
  89. GO  
  90.   
  91. ALTER DATABASE [CompanyDB] SET RECOVERY SIMPLE   
  92. GO  
  93.   
  94. ALTER DATABASE [CompanyDB] SET  MULTI_USER   
  95. GO  
  96.   
  97. ALTER DATABASE [CompanyDB] SET PAGE_VERIFY CHECKSUM    
  98. GO  
  99.   
  100. ALTER DATABASE [CompanyDB] SET DB_CHAINING OFF   
  101. GO  
  102.   
  103. ALTER DATABASE [CompanyDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )   
  104. GO  
  105.   
  106. ALTER DATABASE [CompanyDB] SET TARGET_RECOVERY_TIME = 0 SECONDS   
  107. GO  
  108.   
  109. ALTER DATABASE [CompanyDB] SET  READ_WRITE   
  110. GO   

Create tables

Here, we need to create 2 tables respectively, namely Customers and Orders. 

  1. USE [CompanyDB]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[Customers]    Script Date: 3/11/2017 8:19:22 AM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[Customers](  
  15.     [CustomerID] [varchar](50) NOT NULL,  
  16.     [CompanyName] [varchar](50) NULL,  
  17.     [ContactName] [varchar](50) NULL,  
  18.     [ContactTitle] [varchar](50) NULL,  
  19.     [City] [varchar](50) NULL,  
  20.     [Country] [varchar](50) NULL,  
  21.  CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED   
  22. (  
  23.     [CustomerID] ASC  
  24. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  25. ON [PRIMARY]  
  26.   
  27. GO  
  28.   
  29. SET ANSI_PADDING OFF  
  30. GO  
  31.   
  32.   
  33.   
  34. USE [CompanyDB]  
  35. GO  
  36.   
  37. /****** Object:  Table [dbo].[Orders]    Script Date: 3/11/2017 8:19:38 AM ******/  
  38. SET ANSI_NULLS ON  
  39. GO  
  40.   
  41. SET QUOTED_IDENTIFIER ON  
  42. GO  
  43.   
  44. SET ANSI_PADDING ON  
  45. GO  
  46.   
  47. CREATE TABLE [dbo].[Orders](  
  48.     [OrderID] [intNOT NULL,  
  49.     [CustomerID] [varchar](50) NOT NULL,  
  50.     [EmployeeID] [intNULL,  
  51.     [OrderDate] [dateNULL,  
  52.     [RequiredDate] [dateNULL,  
  53.     [ShippedDate] [dateNULL,  
  54.     [ShipName] [varchar](50) NULL,  
  55.  CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED   
  56. (  
  57.     [OrderID] ASC  
  58. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  59. ON [PRIMARY]  
  60.   
  61. GO  
  62.   
  63. SET ANSI_PADDING OFF  
  64. GO   

After creating the tables, you can add some records, as shown below for the demo.

Customer table


Order table


Create your MVC Application

Open Visual Studio and select File >> New Project.

The New Project Window will pop up. Select ASP.NET Web Application (.NET Framework), name your project and click OK.


Now, a new dialog will pop up to select the template. We are going to choose Web API 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. Dialog box will pop up, inside Visual C#, select Data, followed by ADO.NET Entity Data Model and enter name for your Dbcontext model as CompanyModel and finally click Add.


At this stage, we are going to choose EF Designer from the database, as shown below.


In this 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. Finally, click OK button.




In the next step, the dialog Entity Data Model wizard will pop up to choose the objects which we want to use. In this example, we are going to choose Customers and Orders tables and click Finish button.

Finally, we will see that EDMX model generates Customer and Order entities.




Create a controller

Now, we are going to create a controller. Right click on the controllers folder > Add > Controller> select Web API 2 Controller – Empty > click Add.


Enter Controller name (‘CompanyController’).


CompanyController.cs 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7.   
  8. namespace MasterDetail.Controllers  
  9. {  
  10.     [RoutePrefix("api/Company")]  
  11.     public class CompanyController : ApiController  
  12.     {  
  13.         //DbContext  
  14.         private CompanyDBEntities db = new CompanyDBEntities();  
  15.   
  16.          
  17.         public IQueryable<Customer> GetCustomers()  
  18.         {  
  19.             return db.Customers;  
  20.         }  
  21.   
  22.         [Route("{customerId}")]  
  23.         public IQueryable<Order> GetOrdersByCustomer(string customerId)  
  24.         {  
  25.             return db.Orders.Where(o => o.CustomerID == customerId).AsQueryable();  
  26.         }  
  27.   
  28.   
  29.     }  
  30. }   

As you can see, there are two methods given above, the first thing is used to get all the customers and the second method returns orders related to the customer Id given as a parameter.

Now, we need to add new js file. Right click on scripts folder > Add > JavaScript file.


App.js 

  1. var viewModel = function() {  
  2.   
  3.     var self = this;  
  4.   
  5.     self.CustomerID = ko.observable();  
  6.     self.CompanyName = ko.observable();  
  7.     self.ContactName = ko.observable();  
  8.     self.ContactTitle = ko.observable();  
  9.     self.City = ko.observable();  
  10.     self.Country = ko.observable();  
  11.   
  12.     self.customerList = ko.observableArray([]);  
  13.   
  14.     self.OrderID = ko.observable();  
  15.     self.EmployeeID = ko.observable();  
  16.     self.OrderDate = ko.observable();  
  17.     self.RequiredDate = ko.observable();  
  18.     self.ShippedDate = ko.observable();  
  19.     self.ShipName = ko.observable();  
  20.   
  21.     self.OrdersList = ko.observableArray([]);  
  22.   
  23.     var CompanyUri = '/api/Company/';  
  24.   
  25.   
  26.     function ajaxFunction(uri, method, data) {  
  27.   
  28.         return $.ajax({  
  29.   
  30.             type: method,  
  31.             url: uri,  
  32.             dataType: 'json',  
  33.             contentType: 'application/json',  
  34.             data: data ? JSON.stringify(data) : null  
  35.   
  36.         }).fail(function (jqXHR, textStatus, errorThrown) {  
  37.   
  38.             alert('Error: ' + errorThrown);  
  39.   
  40.         });  
  41.     }  
  42.   
  43.     //Customer List function which returns all customers from database.  
  44.     function customerList() {  
  45.   
  46.         ajaxFunction(CompanyUri, 'GET').done(function (data) {  
  47.   
  48.             self.customerList(data);  
  49.   
  50.         });  
  51.   
  52.     }  
  53.   
  54.     //Detail Orders function accepts customer id as parameter and returns all orders related to customer id.  
  55.     self.detailOrders = function(customer) {  
  56.   
  57.         ajaxFunction(CompanyUri + customer.CustomerID, 'GET').done(function (data) {  
  58.   
  59.             self.OrdersList(data);  
  60.   
  61.         });  
  62.   
  63.     }  
  64.       
  65.     customerList();  
  66.      
  67. };  
  68.   
  69. ko.applyBindings(new viewModel());   

Now, from solution explorer panel, we are going to add MasterDetails.html file.


MasterDetails.html 

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Master Detail :: Template</title>  
  5.     <meta charset="utf-8" />  
  6.     <meta http-equiv="X-UA-Compatible" content="IE=edge">  
  7.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  8.   
  9.     <!--CSS-->  
  10.     <link href="Content/bootstrap.min.css" rel="stylesheet" />  
  11.   
  12. </head>  
  13. <body>  
  14.   
  15.     <nav class="navbar navbar-default navbar-fixed-top">  
  16.   
  17.         <div class="container-fluid">  
  18.   
  19.             <div class="navbar-header">  
  20.                 <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">  
  21.                     <span class="sr-only">Toggle navigation</span>  
  22.                     <span class="icon-bar"></span>  
  23.                     <span class="icon-bar"></span>  
  24.                     <span class="icon-bar"></span>  
  25.                 </button>  
  26.                 <a class="navbar-brand" href="#">Matser Detail - KnockOutJS</a>  
  27.             </div> <!-- END HEADER NAV -->  
  28.   
  29.   
  30.         </div> <!-- END CONTAINER -->  
  31.   
  32.     </nav><!-- END NAV-->  
  33.   
  34.     <div class="container" style="margin-top: 7%;">  
  35.   
  36.   
  37.         <div class="row">  
  38.   
  39.             <div class="col-md-9">  
  40.   
  41.                 <!-- FORM -->  
  42.                 <div class="panel panel-default">  
  43.   
  44.                     <div class="panel-heading"> <span class="glyphicon glyphicon glyphicon-tag" aria-hidden="true"></span> <b>Customer List </b></div>  
  45.                     <div class="panel-body">  
  46.   
  47.                         <table class="table table-hover">  
  48.   
  49.                             <thead>  
  50.                                 <tr>  
  51.                                     <th><span class="glyphicon glyphicon glyphicon-eye-open" aria-hidden="true"></span></th>  
  52.                                     <th>Customer ID</th>  
  53.                                     <th>Company Name</th>  
  54.                                     <th>Contact Name</th>  
  55.                                     <th>Contact Title</th>  
  56.                                     <th>City</th>  
  57.                                     <th>Country</th>  
  58.   
  59.                                 </tr>  
  60.                             </thead> <!-- END THEAD -->  
  61.   
  62.                             <tbody data-bind="foreach: customerList">  
  63.   
  64.                                 <tr>  
  65.   
  66.                                     <td> <button type="button" class="btn btn-default btn-xs" data-bind="click: $root.detailOrders"> <span class="glyphicon glyphicon glyphicon-eye-open" aria-hidden="true"></span></button> </td>  
  67.                                       
  68.                                     <td> <span data-bind="text: CustomerID"></span> </td>  
  69.                                     <td> <span data-bind="text: CompanyName"></span> </td>  
  70.                                     <td> <span data-bind="text: ContactName"></span> </td>  
  71.                                     <td> <span data-bind="text: ContactTitle"></span> </td>  
  72.                                     <td> <span data-bind="text: City"></span> </td>  
  73.                                     <td> <span data-bind="text: Country"></span> </td>  
  74.   
  75.   
  76.                                 </tr>  
  77.   
  78.                             </tbody> <!-- END TBODY -->  
  79.   
  80.                         </table> <!-- END TABLE -->  
  81.   
  82.   
  83.                     </div> <!-- END PANEL BODY-->  
  84.   
  85.                 </div><!-- END PANEL-->  
  86.   
  87.             </div> <!-- END  col-md-8 -->  
  88.   
  89.         </div> <!-- END ROW-->  
  90.   
  91.         <div class="row">  
  92.   
  93.             <div class="col-md-10">  
  94.   
  95.                 <!-- FORM -->  
  96.                 <div class="panel panel-default">  
  97.   
  98.                     <div class="panel-heading"> <span class="glyphicon glyphicon glyphicon-tag" aria-hidden="true"></span> <b>Orders by Customer </b></div>  
  99.                     <div class="panel-body">  
  100.   
  101.                         <table class="table table-hover">  
  102.   
  103.                             <thead>  
  104.                                 <tr>  
  105.   
  106.                                     <th>OrderID</th>  
  107.                                     <th>Customer ID</th>  
  108.                                     <th>Employee ID</th>  
  109.                                     <th>Order Date</th>  
  110.                                     <th>Required Date</th>  
  111.                                     <th>Shipped Date</th>  
  112.                                     <th>Ship Name</th>  
  113.   
  114.                                 </tr>  
  115.                             </thead> <!-- END THEAD -->  
  116.   
  117.                             <tbody data-bind="foreach: OrdersList">  
  118.   
  119.                                 <tr>  
  120.   
  121.                                     <td> <span data-bind="text: OrderID"></span> </td>  
  122.                                     <td> <span data-bind="text: CustomerID"></span> </td>  
  123.                                     <td> <span data-bind="text: EmployeeID"></span> </td>  
  124.                                     <td> <span data-bind="text: OrderDate"></span> </td>  
  125.                                     <td> <span data-bind="text: RequiredDate"></span> </td>  
  126.                                     <td> <span data-bind="text: ShippedDate"></span> </td>  
  127.                                     <td> <span data-bind="text: ShipName"></span></td>  
  128.   
  129.                                 </tr>  
  130.   
  131.                             </tbody>  
  132.   
  133.                         </table>  
  134.   
  135.                     </div> <!-- END PANEL BODY-->  
  136.   
  137.                 </div><!-- END PANEL-->  
  138.   
  139.             </div> <!-- END  col-md-10 -->  
  140.   
  141.         </div> <!-- END ROW-->  
  142.   
  143.     </div> <!-- END CONTAINER-->  
  144.   
  145.     <!-- JS -->  
  146.   
  147.     <script src="Scripts/jquery-1.10.2.min.js"></script>  
  148.     <script src="Scripts/bootstrap.min.js"></script>  
  149.     <script src="Scripts/knockout-3.4.0.js"></script>  
  150.   
  151.     <script src="Scripts/app.js"></script>  
  152. </body>  
  153. </html>   

Note

Don’t forget to add knockout.js library.

<script src="Scripts/knockout-3.4.0.js"></script>

Output

Now, you can run your Application and let’s see the output.


Similar Articles