Introduction

While working with SPA (Single Page Application) using AngularJS in my project, where I need bulk upload to be implemented. I struck for some time and finally achieved this. So, I thought it might be helpful for others who have to implement bulk upload in their app's.

In this demo I have used Visual Studio 2012, .NET Framework 4.5, AngularJS, Bootstrap and .mdf file for the database operations. I have provided all the necessary screenshots and source code to download.

Note:

Please go through my previous demo for the following things to add.

  • Creating MVC project in Visual Studio.

  • Installed Bootstrap, AngularJS and Angular routing from NuGet packages and JavaScript file and name it "myApp" and let it be empty for the time being.

  • Add the above files in RouteConfig.cs.

  • Please you should know about client side routing in AngularJS before going through this demo.

Setting up Database

I will add a .mdf file for the database operations and name it BulkDemo.mdf. I have created a table named BillsClaim. The following is the script of the table.

  1. CREATE TABLE [dbo].[BillsClaim] (  
  2.     [Id]           INT             IDENTITY (1, 1) NOT NULL,  
  3.     [BillNum]      VARCHAR (50)    NULL,  
  4.     [BillAmt]      DECIMAL (18, 2) NULL,  
  5.     [Relationship] VARCHAR (50)    NULL,  
  6.     PRIMARY KEY CLUSTERED ([Id] ASC)  
  7. );  
AngularJS Code:

Just copy the following code into myApp JS file.
  1. var app = angular.module('myApp', ['AngularDemo.HomeController'])  
  2.   
  3. .config(['$routeProvider''$locationProvider', function ($routeProvider, $locationProvider) {  
  4.   
  5.     $routeProvider.when('/', {  
  6.         templateUrl: '/Home/Display',  
  7.         controller: 'homeCtrl',  
  8.     });  
  9.   
  10.     $routeProvider.when('/BulkInsert', {  
  11.         templateUrl: '/Home/BulkInsert',  
  12.         controller: 'homeCtrl',  
  13.     });  
  14.   
  15.     $routeProvider.otherwise({  
  16.         redirectTo: '/'  
  17.     });  
  18.   
  19.     // Specify HTML5 mode (using the History APIs) or HashBang syntax.  
  20.     $locationProvider.html5Mode(false).hashPrefix('!');  
  21.   
  22. }]);  
  23.   
  24.   
  25. //Home Controller  
  26. angular.module('AngularDemo.HomeController', ['ngRoute'])  
  27. .controller('homeCtrl', function ($scope, $http) {  
  28.   
  29.     $scope.TableLength = '';  
  30.     $scope.MaxTableLength = 10;  
  31.     $scope.ReceiptList = '';  
  32.   
  33.     $http.get('/Home/GetClaimsList').success(function (data) {  
  34.         $scope.ReceiptList = data;  
  35.     });  
  36.   
  37.       
  38.     $scope.NewBill =  
  39.         {  
  40.             SingleNewBill: [  
  41.                 {  
  42.                     BillNum: '',  
  43.                     BillAmt: '',  
  44.                     Relationship: ''  
  45.   
  46.                 }  
  47.             ]  
  48.         };  
  49.   
  50.     $scope.ShowRow = function () {  
  51.         var InputIdNo = $scope.NewBill.SingleNewBill.length + 1;  
  52.         if ($scope.MaxTableLength >= InputIdNo) {  
  53.             $scope.NewBill.SingleNewBill.push({ 'id''choice' + InputIdNo });  
  54.         }  
  55.         else {  
  56.             alert("You cannot add more than 10 bills in one go");  
  57.         }  
  58.     };  
  59.   
  60.     $scope.CancelRow = function (Index) {  
  61.         //alert(Index);  
  62.         $scope.NewBill.SingleNewBill.splice(Index, 1);  
  63.     };  
  64.   
  65.     $scope.AddNew = function (tblName) {  
  66.         if ($scope.NewBill.SingleNewBill.length > 0) {  
  67.             $.ajax({  
  68.                 url: '/Home/BulkSave',  
  69.                 type: 'POST',  
  70.                 dataType: 'json',  
  71.                 contentType: 'application/json',  
  72.                 traditional: true,  
  73.                 data: JSON.stringify({ billsClaimModel: $scope.NewBill.SingleNewBill }),  
  74.                 success: function (data) {  
  75.                     //debugger;  
  76.                     alert("Your Medical Bills have been added");  
  77.                     $("#tblName").remove();  
  78.                     $scope.NewBill = "";  
  79.                 }  
  80.             });  
  81.         }  
  82.         else {  
  83.             alert("Please add bill and then save");  
  84.         }  
  85.     };  
  86. });  
Important things for Bulk Upload:

 

  • The way you define your model in AngularJS is important to pass to server side.
  • AngularJS model should match with the model at server side in order to pass data.
  • Rest is up to you on how you will do bulk upload.

AngularJS Model Explanation:

  1. $scope.NewBill =  
  2. {  
  3.     SingleNewBill: [  
  4.         {  
  5.             BillNum: '',  
  6.             BillAmt: '',  
  7.             Relationship: ''  
  8.   
  9.         }  
  10.     ]  
  11. };  
We have a SingleNewBill object, with an array of columns, which is nested in NewBill. When we pass this AngularJS model to server side controller action it should be the list of a model, otherwise the model will be null at server side.

Output Screens

When user clicks on Bulk Insert button the following screen will be displayed.

Bulk Insert button
When user clicks on Add Bill, new table row is created with two text box and dropdown list controls and they can add up to 10 rows max. Here's the screenshot:

Add Bill

Once you click on save button the values are passed to controller action as in the following screenshot. For bulk upload I have used SqlBulkCopy concept of .NET.

SqlBulkCopy

Database

All the values that are entered in UI are stored in the database. The following is the screenshot.

result

HTML Code Explanation

data-ng-repeat is for loop of AngularJS. We are getting each array value and storing in NewBill object and passing it to controller action.

HTML Code Explanation

 

Recommended Free Ebook
Next Recommended Readings