Click here to Skip to main content
15,881,424 members
Articles / Web Development / HTML

MVC AngularJS Master/Detail CRUD, Filter And Sorting Using WEB API 2 with Stored Procedure

Rate me:
Please Sign up or sign in to vote.
4.78/5 (81 votes)
18 Aug 2016CPOL10 min read 175.1K   7.7K   157   51
In this article, you will learn about MVC AngularJS Master/Detail CRUD, Filter and Sorting using WEB API 2 with Stored Procedure.

Image 1

Introduction

In one of my articles, I explained how to create a Master/Detail HTML GRID using MVC and AngularJS. Few members requested me to write an article for Master/Detail HTML grid with CRUD (Insert, Update, Select and Delete) for both Master and Detail grid. As a result, here I have created a simple demo program with the following features.

This article will explain:

  • How to Create Order Master and Order Detail table with sample records inserted
  • Create Stored Procedure to perform Insert/Update/Select and Delete both Order Master and Order Detail table
  • Create Entity Framework and add all the Stored Procedures
  • Create a separate WEB API for both Order Master and Order Detail to execute all our Stored Procedures from AngularJS Controller
  • Create AngularJS Controller to perform all business logic part to display our Master/Detail HTML grid
  • Add Sorting /Filtering features for both Master and Detail HTML grid
  • Display Total Row for each Child Detail Grid
  • Add/Edit/ and Delete each Order Master and Order Detail from grid
  • Search Order Master Details

Prerequisites

You can also view my previous articles related to AngularJS using MVC and the WCF Rest Service:

Previous articles related to Angular JS, MVC and WEB API:

Using the Code

1. Create Database and Table

We will create an Order Master and Order Detail table to be used for the Master and Detail Grid data binding. The following is the script to create a database, table and sample insert query. Run this script in your SQL Server. I have used SQL Server 2014.

SQL
use master
--create DataBase
-- 1) Check for the Database Exists .If the database exists, then drop and create new DB
IF EXISTS (SELECT [name] FROM sys.databases WHERE [name] = 'OrderManagement' )
DROP DATABASE OrderManagement
GO

CREATE DATABASE OrderManagement
GO

USE OrderManagement
GO

-- Create OrderMasters Table

CREATE TABLE [dbo].[OrderMasters](
[Order_No] INT IDENTITY PRIMARY KEY,
[Table_ID] [varchar](20) NOT NULL,
[Description] [varchar](200) NOT NULL,
[Order_DATE] [datetime] NOT NULL,
[Waiter_Name] [varchar](20) NOT NULL
)

-- Insert OrderMasters sample data

INSERT INTO [OrderMasters]
          ([Table_ID] ,[Description],[Order_DATE],[Waiter_Name])
    VALUES
          ('T1','Order for Table T1',GETDATE(),'SHANU' )   

INSERT INTO [OrderMasters]
          ([Table_ID] ,[Description],[Order_DATE],[Waiter_Name])
    VALUES
           ('T2','Order for Table T2',GETDATE(),'Afraz' )        

INSERT INTO [OrderMasters]
          ([Table_ID] ,[Description],[Order_DATE],[Waiter_Name])
     VALUES
             ('T3','Order for Table T3',GETDATE(),'Afreen')                

CREATE TABLE [dbo].[OrderDetails](
  [Order_Detail_No] INT IDENTITY PRIMARY KEY,
 [Order_No] INT,
 [Item_Name] [varchar](20) NOT NULL, 
 [Notes] [varchar](200) NOT NULL,
[QTY]  INT NOT NULL,
 [Price] INT NOT NULL
 )

--Now let’s insert the 3 items for the above Order No 'Ord_001'.

INSERT INTO [OrderDetails]
          ( [Order_No],[Item_Name],[Notes],[QTY] ,[Price])
   VALUES
          (1,'Ice Cream','Need very Cold',2 ,160)

INSERT INTO [OrderDetails]
          ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
   VALUES
          (1,'Coffee','Hot and more Suger',1 ,80)
        
          INSERT INTO [OrderDetails]
          ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
   VALUES
          (1,'Burger','Spicy',3 ,140)
        
          INSERT INTO [OrderDetails]
          ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
   VALUES
          (2,'Pizza','More Chees and Large',1 ,350)        

          INSERT INTO [OrderDetails]
          ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
   VALUES
          (2,'Cola','Need very Cold',3 ,50)
        
          INSERT INTO [OrderDetails]
          ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
   VALUES
          (3,'IDLY','Hot',3 ,40)

          INSERT INTO [OrderDetails]
          ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
   VALUES
          (3,'Thosa','Hot',3 ,50)

-- To Select and test Order Master and Details

Select * FROM OrderMasters
Select * From OrderDetails

After creating our Table, we will create a Stored Procedure to do our CRUD Operations.

First, we create a stored procedure for Order Master Table to perform CRUD.

SQL
-- 1) Stored procedure to Select OrderMaster
-- Author      : Shanu                                                             
-- Create date : 2015-10-26                                                              
-- Description : Order Master                                              
-- Tables used :  OrderMaster                                                              
-- Modifier    : Shanu                                                               
-- Modify date : 2015-10-26                                                               
-- =============================================  
-- exec USP_OrderMaster_Select '',''
-- =============================================
Create PROCEDURE [dbo].[USP_OrderMaster_Select]                                            
   (                          
     @OrderNo           VARCHAR(100)     = '',
     @Table_ID               VARCHAR(100)     = ''  
      )                                                      
AS                                                              
BEGIN      
         Select [Order_No],
                [Table_ID],
                [Description],
                [Order_DATE],
                [Waiter_Name]
            FROM
                OrderMasters
            WHERE
                Order_No like  @OrderNo +'%'
                AND Table_ID like @Table_ID +'%'
            ORDER BY
                Table_ID  
END

-- 2) Stored procedure to insert OrderMaster
-- Author      : Shanu                                                              
-- Create date : 2015-10-26                                                             
-- Description : Order Master                                             
-- Tables used :  OrderMaster                                                              
-- Modifier    : Shanu                                                               
-- Modify date : 2015-10-26                                                                 
-- =============================================   
-- exec USP_OrderMaster_Insert 'T4','Table 4','SHANU'
-- =============================================
Create PROCEDURE [dbo].[USP_OrderMaster_Insert]                                             
   (                      
     @Table_ID           VARCHAR(100)     = '',
     @Description               VARCHAR(100)     = '',
     @Waiter_Name               VARCHAR(20)     = ''
      )                                                       
AS                                                               
BEGIN       
        IF NOT EXISTS (SELECT Table_ID FROM OrderMasters WHERE Table_ID=@Table_ID)
            BEGIN
                  INSERT INTO [OrderMasters]
          ([Table_ID] ,[Description],[Order_DATE],[Waiter_Name])
    VALUES
          (@Table_ID,@Description,GETDATE(),@Waiter_Name )                           

                  Select 'Inserted' as results                     
         END
         ELSE
             BEGIN
                     Select 'Exists' as results
              END
END

-- 3) Stored procedure to Update OrderMaster
   
-- Author      : Shanu                                                               
-- Create date : 2015-10-26                                                              
-- Description : Order Master                                             
-- Tables used :  OrderMaster                                                              
-- Modifier    : Shanu                                                               
-- Modify date : 2015-10-26                                                               
-- =============================================     
-- exec USP_OrderMaster_Update 4,'T4','Table 4 wer','SHANU'
-- =============================================
CREATE PROCEDURE [dbo].[USP_OrderMaster_Update]                                             
   (  @OrderNo               Int=0,                          
      @Table_ID           VARCHAR(100)     = '',
      @Description               VARCHAR(100)     = '',
      @Waiter_Name               VARCHAR(20)     = ''
      )                                                       
AS                                                               
BEGIN      
        IF NOT EXISTS (SELECT Table_ID FROM OrderMasters _
        WHERE Order_No!=@OrderNo AND Table_ID=@Table_ID)
            BEGIN
                    UPDATE OrderMasters
                    SET    [Table_ID]=@Table_ID ,
                           [Description]=@Description,
                           [Order_DATE]=GETDATE(),
                           [Waiter_Name]=@Waiter_Name

                    WHERE
                        Order_No=@OrderNo                             
                    Select 'updated' as results                       
            END
         ELSE
             BEGIN
                     Select 'Exists' as results
              END
END

-- 4) Stored procedure to Delete OrderMaster 

-- Author      : Shanu                                                               
-- Create date : 2015-10-26                                                              
-- Description : Order Master                                             
-- Tables used :  OrderMaster                                                               
-- Modifier    : Shanu                                                               
-- Modify date : 2015-10-26                                                                
-- ============================================= 
-- exec USP_OrderMaster_Delete '3'
-- =============================================
CREATE PROCEDURE [dbo].[USP_OrderMaster_Delete]                                             

   (  @OrderNo               Int=0 )                                                        
AS                                                               
BEGIN       
        DELETE FROM OrderMasters WHERE           Order_No=@OrderNo            
                           DELETE from OrderDetails WHERE  Order_No=@OrderNo    

                            Select 'Deleted' as results

END

Next, we create stored procedure for Order Detail Table to perform CRUD.

SQL
USE OrderManagement
GO

-- 1) Stored procedure to Select OrderDetails
-- Author      : Shanu                                                               
-- Create date : 2015-10-26                                                              
-- Description : OrderDetails                                           
-- Tables used :  OrderDetails                                                              
-- Modifier    : Shanu                                                               
-- Modify date : 2015-10-26                                                               
-- =============================================  
-- exec USP_OrderDetail_Select '1'
-- =============================================
Create PROCEDURE [dbo].[USP_OrderDetail_Select]                                              
   (                           
     @OrderNo           VARCHAR(100)     = '' 
      )                                                       
AS                                                               
BEGIN       
         Select Order_Detail_No,
                 [Order_No],
                [Item_Name],
                [Notes],
                [QTY],
                [Price]
            FROM
                OrderDetails
            WHERE
                Order_No like  @OrderNo +'%'            
            ORDER BY
                Item_Name   
END

-- 2) Stored procedure to insert OrderDetail
-- Author      : Shanu                                                               
-- Create date : 2015-10-26                                                               
-- Description : Order Master                                             
-- Tables used :  OrderDetail                                                              
-- Modifier    : Shanu                                                                
-- Modify date : 2015-10-26                                                                 
-- =============================================   
-- exec USP_OrderDetail_Insert 4,'cadburys','cadburys Chocolate','50',50
-- =============================================
Create PROCEDURE [dbo].[USP_OrderDetail_Insert]                                             
   ( 
     @Order_No                                          VARCHAR(10),                    
     @Item_Name           VARCHAR(100)     = '',
     @Notes               VARCHAR(100)     = '',
     @QTY                 VARCHAR(20)     = '',
     @Price               VARCHAR(20)     = ''
      )                                                       
AS                                                              
BEGIN       
        IF NOT EXISTS (SELECT Item_name FROM OrderDetails _
           WHERE Order_No=@Order_No AND Item_Name=@Item_Name)
            BEGIN
                  INSERT INTO [OrderDetails]
          ( [Order_No],[Item_Name],[Notes],[QTY] ,[Price])
    VALUES
          ( @Order_No,@Item_Name,@Notes,@QTY ,@Price )
                    Select 'Inserted' as results                       
            END
         ELSE
             BEGIN
                     Select 'Exists' as results
              END
END

-- 3) Stored procedure to Update OrderDetail
  
-- Author      : Shanu                                                               
-- Create date : 2015-10-26                                                              
-- Description : Order Master                                             
-- Tables used :  OrderDetail                                                              
-- Modifier    : Shanu                                                               
-- Modify date : 2015-10-26                                                                
-- =============================================     
-- exec USP_OrderDetail_Update 8,4,'Cadburys','cadburys Chocolate','50',50
-- =============================================
Create PROCEDURE [dbo].[USP_OrderDetail_Update]                                             
   (  @Order_Detail_No   Int=0,                          
      @Order_No                                        VARCHAR(10),                    
      @Item_Name           VARCHAR(100)     = '',
      @Notes               VARCHAR(100)     = '',
      @QTY                 VARCHAR(20)     = '',
      @Price               VARCHAR(20)     = ''
      )                                                      
AS                                                                
BEGIN       
        IF NOT EXISTS (SELECT Item_name FROM OrderDetails _
           WHERE Order_Detail_No!=@Order_Detail_No AND Item_Name=@Item_Name)
            BEGIN
                    UPDATE OrderDetails
                    SET   [Item_Name]=@Item_Name,
                                                          [Notes]=@Notes,
                                                                   [QTY] =@QTY,
                                                                  [Price]=@Price
                    WHERE
                       Order_Detail_No=@Order_Detail_No
                            AND  Order_No=@Order_No
                    Select 'updated' as results                      
            END
         ELSE
             BEGIN
                     Select 'Exists' as results
              END
END
-- 4) Stored procedure to Delete OrderDetail
   
-- Author      : Shanu                                                               
-- Create date : 2015-10-26                                                              
-- Description : Order Master                                             
-- Tables used :  OrderDetail                                                              
-- Modifier    : Shanu                                                               
-- Modify date : 2015-10-26                                                                 
-- ============================================= 
-- exec USP_OrderDetail_Delete '8'
-- ============================================
CREATE PROCEDURE [dbo].[USP_OrderDetail_Delete]                                             
   (  @Order_Detail_No               Int=0 )
AS                                                               
BEGIN       
                            DELETE from OrderDetails WHERE _
                                   Order_Detail_No=@Order_Detail_No
                            Select 'Deleted' as results          

END

2. Create Your MVC Web Application in Visual Studio 2015

After installing our Visual Studio 2015, click Start, then Programs and select Visual Studio 2015 - Click Visual Studio 2015.

Click New, then Project, select Web and click ASP.NET Web Application. Select your project location and enter your web application name.

Click New, then Project, select Web and click ASP.NET Web Application. Select your project location and enter your web application name.

Image 2

Select MVC and in Add Folders and Core reference for select the Web API and click OK.

Image 3

Add Database using ADO.NET Entity Data Model

Right click our project and click Add, then New Item.

Image 4

Select Data, then ADO.NET Entity Data Model and give the name for our EF and click Add.

Image 5

Select EF Designer from the database and click Next >.

Image 6

Here, click New Connection and provide your SQL Server - Server Name and connect to your database.

Image 7

Here, we can see I have given my SQL server name, Id and PWD and after it connected, I selected the database as OrderManagement as we have created the database using my SQL Script.

Image 8

Click Next and select the tables and all Stored Procedures need to be used and click Finish.

Image 9

Here, we can see now we have created our OrderDetailModel.

Image 10

Once the Entity has been created, the next step is to add a Web API to our controller and write function to Select/Insert/Update and Delete.

Procedure to Add Our Web API Controller

Right-click the Controllers folder, click Add and then click Controller.

Image 11

Select Controller and add an Empty Web API 2 Controller. Provide your name to the Web API controller and click OK. Here for my Web API Controller, I have given the name “OrderAPIController”. In this demo project, I have created two different controllers for Order master and Order detail.

As we all know, Web API is a simple and easy way to build HTTP Services for Browsers and Mobiles.

Web API has the following four methods as Get/Post/Put and Delete where:

  • Get is to request for the data (Select)
  • Post is to create a data (Insert)
  • Put is to update the data
  • Delete is to delete data

Get Method

In our example, I have used only a Get method since I am using only a Stored Procedure. We need to create an object for our Entity and write our Get Method to do Select/Insert/Update and Delete operations.

Select Operation

We use a get method to get all the details of the OrderMasters table using an entity object and we return the result as an IEnumerable. We use this method in our AngularJs and display the result in an MVC page from the AngularJs controller. Using Ng-Repeat, we can bind the details.

Here, we can see in the get method, I have passed the search parameter to the USP_OrderMaster_Select Stored Procedure. In the Stored Procedure, I used like "%" to return all the records if the search parameter is empty.

C#
OrderManagementEntities objapi = new OrderManagementEntities();

                           // to Search Student Details and display the result
                           [HttpGet]
  public IEnumerable<USP_OrderMaster_Select_Result> Get(string OrderNO, string TableID)
      {
            if (OrderNO == null)
                   OrderNO = "";
             if (TableID == null)
                   TableID = "";
           return objapi.USP_OrderMaster_Select(OrderNO, TableID).AsEnumerable();
                           }

Here in my example, I have used the get method for Select/Insert/Update and Delete operations, since in my Stored Procedure after insert/update and delete, I have returned the message from the database.

Insert Operation

Similar to select, I passed all the parameters to the insert procedure. This insert method will return the result from the database as a record is inserted or maybe not. I will get the result and display it from the AngularJs Controller to MVC application.

C#
// To Insert new Student Details
               [HttpGet]
   public IEnumerable<string> insertOrderMaster
          (string Table_ID,string Description,string Waiter_Name)
      {
           return objapi.USP_OrderMaster_Insert( Table_ID, Description, 
                  Waiter_Name).AsEnumerable();
      }

Update Operation

Similar to Insert, I have passed all the parameters to the insert procedure. This Update method will return the result from the database as a record is updated or maybe not. I will pass the OrderNo to the update procedure to update the record for the OrderNo. I will get the result and display it from the AngularJs Controller to the MVC application.

C#
//to Update Student Details
               [HttpGet]
    public IEnumerable<string> updateOrderMaster(int OrderNo, 
           string Table_ID, string Description, string Waiter_Name)
      {
    return objapi.USP_OrderMaster_Update(OrderNo, Table_ID, 
           Description, Waiter_Name).AsEnumerable();
       }

Delete Operation

Similar to update, I have passed the OrderNo to the procedure to delete the record.

C#
//to Delete Student Details
   [HttpGet]
   public IEnumerable<string> deleteOrderMaster(int OrderNo)
    {
        return objapi.USP_OrderMaster_Delete(OrderNo).AsEnumerable();
    }

Similar to OrderMasterController, I have created another controller as “DetailAPI” for Detail table CRUD Operations. Here is the complete code for detailController.

C#
public class DetailAPIController : ApiController
    {
          OrderManagementEntities objapi = new OrderManagementEntities();
       // to Search Student Details and display the result
 [HttpGet]
   public IEnumerable<USP_OrderDetail_Select_Result> Get(string OrderNO)
   {
         if (OrderNO == null)
            OrderNO = "0";
           return objapi.USP_OrderDetail_Select(OrderNO).AsEnumerable();
           }
             // To Insert new Student Details
               [HttpGet]
 public IEnumerable<string> insertOrderDetail
        (string Order_No, string Item_Name, string Notes, string QTY, string Price)
  {
  return objapi.USP_OrderDetail_Insert
         (Order_No, Item_Name, Notes, QTY, Price).AsEnumerable();
               }

               //to Update Student Details
               [HttpGet]
               public IEnumerable<string> updateOrderDetail(int Order_Detail_No, 
               string Order_No, string Item_Name, string Notes, string QTY, string Price)
               {
          return objapi.USP_OrderDetail_Update(Order_Detail_No, Order_No, Item_Name, 
                                               Notes, QTY, Price).AsEnumerable();
               }

               //to Delete Student Details
  [HttpGet]
   public IEnumerable<string> deleteOrderDetail(int Order_Detail_No)
         {
             return objapi.USP_OrderDetail_Delete(Order_Detail_No).AsEnumerable();
      }
       }

Now we have created our Web API Controller Class. The next step is to create our AngularJs Module and Controller. Let's see how to create our AngularJs Controller. In Visual Studio 2015, it's much easier to add our AngularJs Controller. Let's see step-by-step how to create and write our AngularJs Controller.

Creating AngularJs Controller

First, create a folder inside the Script Folder and I have given the folder name as “MyAngular”.

MyAngular Folder

Now add your Angular Controller inside the folder.

Right-click the MyAngular folder and click Add and New Item. Select Web and then AngularJs Controller and provide a name for the Controller. I have named my AngularJs Controller “Controller.js”.

AngularJs Controller

Once the AngularJs Controller is created, we can see by default, the controller will have the code with the default module definition and all.

controller

I have changed the preceding code like adding a Module and controller as in the following.

If the AngularJs package is missing, then add the package to your project.

Right-click your MVC project and click Manage NuGet Packages. Search for AngularJs and click Install.

angularJS

Procedure to Create AngularJs Script Files

Modules.js: Here, we will add the reference to the AngularJs JavaScript and create an Angular Module named “RESTClientModule”.

JavaScript
// <reference path="../angular.js" /> 
/// <reference path="../angular.min.js" />  
/// <reference path="../angular-animate.js" />  
/// <reference path="../angular-animate.min.js" /> 

var app;

(function () {
    app = angular.module("RESTClientModule", ['ngAnimate']);
})();

Controllers: In AngularJs Controller, I have done all the business logic and returned the data from Web API to our MVC HTML page.

1. Variable Declarations

First, I declared all the local variables that need to be used.

JavaScript
app.controller("AngularJs_studentsController", 
                function ($scope, $timeout, $rootScope, $window, $http) {

    $scope.date = new Date();
    $scope.MyName = "shanu";

    //For Order Master Search
    $scope.OrderNos = "";
    $scope.Table_IDs = "";  

    //This variable will be used for Insert/Edit/Delete OrderMasters Table.
    $scope.OrderNo = 0;
    $scope.Table_ID = "";
    $scope.Description = "";
    $scope.Waiter_Name = "";
    //Show Hide OrderMaster Table

    $scope.showOrderMasterAdd = true;
    $scope.addEditOrderMaster = false;
    $scope.OrderMasterList = true;
    $scope.showItem = true;

    //This variable will be used for Insert/Edit/Delete OrderDetail Table.
    $scope.Order_Detail_No = 0;
    $scope.Item_Name ="";
    $scope.Notes = "";
    $scope.QTY = "1";
    $scope.Price = "0";  

    $scope.addEditOrderDetail = false;
    $scope.expandImg = "expand.png";

2. Methods

Select Method

In the select method, I have used $http.get to get the details from Web API. In the get method, I will provide our API Controller name and method to get the details. Here, we can see I have passed the search parameter of OrderNO and TableID using:

JavaScript
{ params: { OrderNO: OrderNos, TableID: Table_IDs }

The final result will be displayed to the MVC HTML page using data-ng-repeat.

JavaScript
$http.get('/api/OrderAPI/', { params: 
{ OrderNO: OrderNos, TableID: Table_IDs } }).success(function (data) {

            $scope.OrderMasters = data;
            $scope.showOrderMasterAdd = true;
            $scope.addEditOrderMaster = false;
            $scope.OrderMasterList = true;
            $scope.showItem = true;
            $scope.addEditOrderDetail = false;
            if ($scope.OrderMasters.length > 0) {
            }
        })
   .error(function () {
       $scope.error = "An Error has occurred while loading posts!";
   });
    }

Search Button Click

In the search button click, I will call the SearchMethod to bind the result. Here, we can see in the search text box I have used ng-model="OrderNos". Using ng-model in the AngularJs Controller, we can get the TextBox input value or we can set the value to the TextBox.

HTML
<input type="text" name="txtOrderNos" ng-model="OrderNos" value="" />
<input type="text" name="txtTable_IDs" 
ng-model="Table_IDs" /><input type="submit" value="Search" 
style="background-color:#336699;color:#FFFFFF" ng-click="searchOrderMasters()" />

//Search
    $scope.searchOrderMasters = function () {
        selectOrderMasters($scope.OrderNos, $scope.Table_IDs);
    }

Image 16

Insert New Order Master

In the ADD New Student Detail button click, I will make visible the StudentAdd table details where the user can enter the new student information. For a new student, I will make the Student ID as 0. In the New Student save button click, I will call the save method.

JavaScript
// New Student Add Details
    $scope.showOrderMasters = function () {
        cleardetails();
        $scope.addEditOrderDetail = false;
        $scope.showOrderMasterAdd = true;
        $scope.addEditOrderMaster = true;
        $scope.OrderMasterList = true;
        $scope.showItem = true;
    }

In the Save method, I will check for the OrderNo. If the OrderNo is “0”, then it will insert the new Order Master details. Here, I will call the Insert Web API method and if the OrderNo is > 0, then that means that to update the Order record, I will call the Update Web API method.

Image 17

To the Insert Web API method, I will pass all the Input parameters. In my Stored Procedure, I will check whether the Table Name for the Order already exists. If the Table name does not exist in the database, then I will insert the records and return the success message as “inserted” and if the Table name already exists, then I will return the message as “Exists”.

JavaScript
//Save OrderMaster
    $scope.saveDetails = function () {
        $scope.IsFormSubmitted1 = true;
        if ($scope.IsFormValid1) {        
             if ($scope.OrderNo == 0) {
                $http.get('/api/OrderAPI/insertOrderMaster/', 
                { params: { Table_ID: $scope.Table_ID, Description: $scope.Description, 
                Waiter_Name: $scope.Waiter_Name } }).success(function (data) {
                    $scope.orderMasterInserted = data;
                    alert($scope.orderMasterInserted);
                    cleardetails();
                    selectOrderMasters('', '');
                })
         .error(function () {
             $scope.error = "An Error has occurred while loading posts!";
         });
            }
            else {  // to update to the student details
                $http.get('/api/OrderAPI/updateOrderMaster/', 
                { params: { OrderNo: $scope.OrderNo, Table_ID: $scope.Table_ID, 
                Description: $scope.Description, Waiter_Name: $scope.Waiter_Name } }).
                success(function (data) {
                    $scope.orderMasterUpdated = data;
                    alert($scope.orderMasterUpdated);
                    cleardetails();
                    selectOrderMasters('', '');
                })
        .error(function () {
            $scope.error = "An Error has occurred while loading posts!";
        });
            }
        }
        else {
            $scope.Message1 = "All the fields are required.";
        }   
}

Update Order Master

Image 18

The same as Insert, I will display the update details for the user to edit the details and save it. In the Edit method, I will get all the details for the row where the user clicks on the Edit Icon and sets all the results to the appropriate TextBox. In the Save button click, I will call the save method to save all the changes to the database like Insert.

JavaScript
//Edit Order Details
 $scope.OrderMasterEdit = function OrderMasterEdit
        (OrderNoss, Table_IDss, Descriptionss, Waiter_Namess) {
        cleardetails();
        $scope.OrderNo = OrderNoss;
        $scope.Table_ID = Table_IDss
        $scope.Description = Descriptionss;
        $scope.Waiter_Name = Waiter_Namess;   

        $scope.addEditOrderDetail = false;
        $scope.showOrderMasterAdd = true;
        $scope.addEditOrderMaster = true;
        $scope.OrderMasterList = true;
        $scope.showItem = true;
    }

Delete Order Master Details

Image 19

In the Delete button click, I will display the confirmation message to the user as to whether to delete the Order or not. If the user clicks the OK button, I will pass the OrderNo to the delete method of the Web API to delete the record from the database.

JavaScript
//Delete Order master Detail
    $scope.OrderMasterDelete = function OrderMasterDelete(OrderNoss) {
        cleardetails();
        $scope.OrderNo = OrderNoss;
    var delConfirm = confirm("Are you sure you want to delete the Order Master " + 
                              OrderNoss + " ?");
        if (delConfirm == true) {
         //   alert($scope.OrderNo);
            $http.get('/api/OrderAPI/deleteOrderMaster/', 
            { params: { OrderNo: $scope.OrderNo } }).success(function (data) {
               // alert(data);
                $scope.orderMasterDeleted= data;
                alert($scope.orderMasterDeleted);
                cleardetails();
                selectOrderMasters('', '');
            })
      .error(function () {
          $scope.error = "An Error has occurred while loading posts!";
      });
        }
    }

Filter and Sorting Order Master

Image 20

The filters can be added with the ng-repeat using the pipe symbol.

Here, we can see with ng-repeat, we have added the filter and for the filter we have given the TextBox Model id. When the user presses the key on the TextBox, the filter will be applied for the loop and display the appropriate value as in the following:

HTML
</tr>
   <tr style="height: 30px; background-color:#336699 ; 
   color:#FFFFFF ;border: solid 1px #659EC7;">
                        <td width="100" 
                        align="center" colspan="3">
                            <img src="~/Images/filter.png" />  Filter By
                        </td>
 <td width="180" align="center" 
 style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
                            <input ng-model="search.Order_No" 
                            placeholder="Order..." width="90">
                        </td>
 <td width="180" align="center" style="border: 
 solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
                            <input ng-model="search.Table_ID" 
                            placeholder="Table...">
                        </td>
 <td width="200" align="center" style="border: 
 solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
                </td>
   <td width="200" align="center" style="border: 
   solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
                  </td>
   <td width="200" align="center" style="border: 
   solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
                           <input ng-model="search.Waiter_Name" 
                           placeholder="Name...">
                        </td>
                    </tr>

Sorting Order Master

The same as for a filter, we add the orderBy with field and reverse value in ng-repeat using the pipe symbol.

The OrderBy can be added with the ng-repeat using the pipe symbol, for example, let's consider the preceding example.

And in ng-repeat, we will be giving the search by filter which will be filters all the textbox values which we enter and produce the filtered result.

HTML
<tbody data-ng-repeat="stds in OrderMasters | filter:search | orderBy:predicate:reverse">

Displaying Order Detail

Image 21

Here, we can see how I have displayed the Order Detail grid inside the Order Master by clicking the Detail button click.

In each Order Master Row click, I will check for the active row. And then I will detail button click I call the showNewOrderDetails() method to display the details.

HTML
<tr ng-show="activeRow==stds.Order_No" >

In detail button click, I call the:

HTML
<input type="button" value="Add Detail" 
style="background-color:#439633;color:#FFFFFF;
font-size:large;width:100px;border-color:#a2aabe;
border-style:dashed;border-width:2px;" ng-click="showNewOrderDetails()" />

// New Detail Add
    $scope.showNewOrderDetails = function () {      
        clearOrderdetails();
        $scope.showOrderMasterAdd = false;
        $scope.addEditOrderMaster = false;
        $scope.OrderMasterList = true;
        $scope.showItem = true;
        $scope.addEditOrderDetail = true;
    }

For order Detail CRUD, Sorting and Filtering the same logic as we have seen for Order master has been used. Here, we will see output of:

Order Detail Add

Image 22

Order Detail Edit

Image 23

Order Detail Delete

Image 24

Order Detail Filtering and Sorting

Image 25

Points of Interest

The main aim of this article is to create a simple MVC Web Based Master/Detail CRUD, Filtering and Sorting Operation using Angular JS WEB API 2 with Stored Procedure.

Supported Browsers: Chrome and Firefox

History

  • 2nd November, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog

My Interview on Microsoft TechNet Wiki Ninja Link

Comments and Discussions

 
QuestionIs sproc USP_OrderMaster_Update correct? Pin
Red Feet2-Nov-15 23:19
Red Feet2-Nov-15 23:19 
AnswerRe: Is sproc USP_OrderMaster_Update correct? Pin
PANKAJMAURYA2-Nov-15 23:28
professionalPANKAJMAURYA2-Nov-15 23:28 
SuggestionRe: Is sproc USP_OrderMaster_Update correct? Pin
Red Feet2-Nov-15 23:50
Red Feet2-Nov-15 23:50 
GeneralRe: Is sproc USP_OrderMaster_Update correct? Pin
syed shanu2-Nov-15 23:56
mvasyed shanu2-Nov-15 23:56 
GeneralRe: Is sproc USP_OrderMaster_Update correct? Pin
syed shanu3-Nov-15 13:15
mvasyed shanu3-Nov-15 13:15 
AnswerRe: Is sproc USP_OrderMaster_Update correct? Pin
syed shanu3-Nov-15 13:13
mvasyed shanu3-Nov-15 13:13 
PraiseRe: Is sproc USP_OrderMaster_Update correct? Pin
Red Feet3-Nov-15 22:09
Red Feet3-Nov-15 22:09 
GeneralRe: Is sproc USP_OrderMaster_Update correct? Pin
Jimmy_James_J4-Nov-15 14:38
Jimmy_James_J4-Nov-15 14:38 
I agree with Red Feet. This is a bit weird. I would just do.
SQL
IF EXISTS (SELECT Order_No FROM OrderMasters WHERE Order_No=@OrderNo)

As in your Where clause you use:
SQL
WHERE Order_No=@OrderNo   

Try not to use Negative if statements as the are harder to follow. You have a Not exists and a != hard to understand what you are getting at.
GeneralNICE ARTICLE Pin
gandhichintan2-Nov-15 20:53
professionalgandhichintan2-Nov-15 20:53 
GeneralRe: NICE ARTICLE Pin
syed shanu3-Nov-15 13:20
mvasyed shanu3-Nov-15 13:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.