This demo is a continuation of my previous article Single Page Application with AngularJs in. NET.
Please go through the previous demo before going through this.
In the last demo, I showed inserting data into and displaying data from a database. In this demo, I will show the update and delete functionalities in a SPA using AngularJs. I have provided the source code for CRUD operations to download.
AngularJs Code
The following is the complete AngularJs code.
angular.module('App', ['AngularDemo.EmpAddController', 'AngularDemo.AddressController', 'AngularDemo.DeleteController'])
.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider.when('/', {
templateUrl: '/Home/AddEmployee',
controller: 'EmpAddCtrl',
});
$routeProvider.when('/Edit', {
templateUrl: '/Home/EditEmployee',
controller: 'EditCtrl'
});
$routeProvider.when('/Delete', {
templateUrl: '/Home/DeleteEmployee',
controller: 'DeleteCtrl'
});
$routeProvider.otherwise({
redirectTo: '/'
});
// Specify HTML5 mode (using the History APIs) or HashBang syntax.
$locationProvider.html5Mode(false).hashPrefix('!');
}]);
//Add Employee Controller
angular.module('AngularDemo.EmpAddController', ['ngRoute'])
.controller('EmpAddCtrl', function ($scope, $http) {
$scope.EmpAddressList = {};
$http.get('/Home/ShowEmpList').success(function (data) {
$scope.EmpAddressList = data;
});
$scope.EmpDetailsModel =
{
EmpID: '',
EmpName: '',
EmpPhone: ''
};
$scope.EmpAddressModel =
{
Address1: '',
Address2: '',
Address3: ''
};
$scope.EmployeeViewModel = {
empDetailModel: $scope.EmpDetailsModel,
empAddressModel: $scope.EmpAddressModel
};
$scope.AddEmployee = function () {
//debugger;
$.ajax({
url: '/Home/AddEmpDetails',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
traditional: true,
cache: false,
data: JSON.stringify({ EmployeeViewModelClient: $scope.EmployeeViewModel }),
success: function (data) {
$scope.EmpAddressList.push(data[0]);
$scope.$apply();
//$scope.$apply();
alert("Record is been added");
}
});
};
});
//Address Controller
angular.module('AngularDemo.AddressController', ['ngRoute'])
.controller('EditCtrl', function ($scope, $http) {
$scope.EmpAddressList = {};
$http.get('/Home/ShowEmpList').success(function (data) {
$scope.EmpAddressList = data;
});
$scope.EmpDetailsModel =
{
EmpID: '',
EmpName: '',
EmpPhone: ''
};
$scope.EmpAddressModel =
{
Address1: '',
Address2: '',
Address3: ''
};
$scope.EmployeeViewModel = {
empDetailModel: $scope.EmpDetailsModel,
empAddressModel: $scope.EmpAddressModel
};
$scope.EditEmployee = function (EmployeeID) {
$.ajax({
url: '/Home/GetEmployeeById',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
traditional: true,
data: JSON.stringify({ EmpID: EmployeeID }),
cache: false,
success: function (data) {
$scope.EmpDetailsModel.EmpID = data[0].empDetailModel.EmpID;
$scope.EmpDetailsModel.EmpName = data[0].empDetailModel.EmpName;
$scope.EmpDetailsModel.EmpPhone = data[0].empDetailModel.EmpPhone;
$scope.EmpAddressModel.Address1 = data[0].empAddressModel.Address1
$scope.EmpAddressModel.Address2 = data[0].empAddressModel.Address2;
$scope.EmpAddressModel.Address3 = data[0].empAddressModel.Address3;
$scope.$apply();
}
});
};
$scope.UpdateEmployee = function () {
$.ajax({
url: '/Home/UpdateEmployee',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
traditional: true,
data: JSON.stringify({ EmployeeViewModelClient: $scope.EmployeeViewModel }),
cache: false,
success: function (data) {
$scope.EmpAddressList = data;
$scope.$apply();
}
});
};
});
angular.module('AngularDemo.DeleteController', ['ngRoute'])
.controller('DeleteCtrl', function ($scope, $http) {
//$scope.Message = "Delete in Part 2 is coming soon";
$scope.EmpAddressList = {};
$http.get('/Home/ShowEmpList').success(function (data) {
$scope.EmpAddressList = data;
});
$scope.DeleteByID = function (EmployeeID) {
$.ajax({
url: '/Home/DeleteByID',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
traditional: true,
cache: false,
data: JSON.stringify({ EmpID: EmployeeID }),
success: function (data) {
$scope.EmpAddressList = data;
$scope.$apply();
}
});
};
});
Here I will explain what client-side routing is and some AngularJs directives because I have already done that in my previous demo.
AngularJs Edit controller
angular.module('AngularDemo.AddressController', ['ngRoute'])
.controller('EditCtrl', function ($scope, $http) {
$scope.EmpAddressList = {};
$http.get('/Home/ShowEmpList').success(function (data) {
$scope.EmpAddressList = data;
});
$scope.EmpDetailsModel =
{
EmpID: '',
EmpName: '',
EmpPhone: ''
};
$scope.EmpAddressModel =
{
Address1: '',
Address2: '',
Address3: ''
};
$scope.EmployeeViewModel = {
empDetailModel: $scope.EmpDetailsModel,
empAddressModel: $scope.EmpAddressModel
};
$scope.EditEmployee = function (EmployeeID) {
$.ajax({
url: '/Home/GetEmployeeById',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
traditional: true,
data: JSON.stringify({ EmpID: EmployeeID }),
cache: false,
success: function (data) {
$scope.EmpDetailsModel.EmpID = data[0].empDetailModel.EmpID;
$scope.EmpDetailsModel.EmpName = data[0].empDetailModel.EmpName;
$scope.EmpDetailsModel.EmpPhone = data[0].empDetailModel.EmpPhone;
$scope.EmpAddressModel.Address1 = data[0].empAddressModel.Address1;
$scope.EmpAddressModel.Address2 = data[0].empAddressModel.Address2;
$scope.EmpAddressModel.Address3 = data[0].empAddressModel.Address3;
$scope.$apply();
}
});
};
$scope.UpdateEmployee = function () {
$.ajax({
url: '/Home/UpdateEmployee',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
traditional: true,
data: JSON.stringify({ EmployeeViewModelClient: $scope.EmployeeViewModel }),
cache: false,
success: function (data) {
$scope.EmpAddressList = data;
$scope.$apply();
}
});
};
});








Zeeshan AzimPosted Dec 29, 2015, 8:24 AM
Very Nice and informative article. Thanks for sharing
Hashim ShafiqPosted Aug 30, 2015, 9:43 PM
nice
Azeem AnzariPosted Aug 29, 2015, 1:25 PM
Well explained
Pankaj Kumar ChoudharyPosted Aug 29, 2015, 8:06 AM
nice article..........
deepak vermaPosted Aug 29, 2015, 7:47 AM
Awesome
Ratnesh SinghPosted Aug 28, 2015, 11:56 PM
Nice..
Gopi ChandPosted Aug 28, 2015, 11:47 PM
Nice work
Karthikeyan KPosted Aug 28, 2015, 9:29 PM
Good one...Thanks for sharing