I am integrating a WCF REST service with an AngularJS application. My goal is to retrieve account information based on the account number provided, however, I am encountering an issue where the text "Account_Type" is displayed three times before showing the actual values.
Below is the code snippet for the method:
public string AccountDetails(string Account_Number)
{
var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var inOut = context.Current_Account_Deposit.Where(x => x.Account_Number == accountNumber).Select(w => new AccountTransaction
{
Account_Number = w.Account_Number,
Account_Balance = (decimal?)0M,
Deposit = (decimal?)w.Amount,
Withdrawal = (decimal?)null,
Date = w.Date,
Account_Type=null,
Account_Holder_Tittle = null,
Account_Holder_FirstName =null,
Account_Holder_LastName = null
}).Union(context.Current_Account_Withdraw.Where(x => x.Account_Number == accountNumber).Select(d => new AccountTransaction
{
Account_Number = d.Account_Number,
Account_Balance = (decimal?)0M,
Deposit = (decimal?)null,
Withdrawal = (decimal?)d.Amount,
Date = d.Date,
Account_Type = null,
Account_Holder_Tittle = null,
Account_Holder_FirstName = null,
Account_Holder_LastName = null
})).OrderBy(r => r.Date)
.Union(context.Current_Account_Details.Where(x => x.Account_Number == accountNumber).Select(e => new AccountTransaction
{
Account_Number = e.Account_Number,
Account_Balance = (decimal?)e.Account_Balance,
Deposit = (decimal?)0M,
Withdrawal = (decimal?)0M,
Date = e.Account_Creation_Date,
Account_Type=e.Account_Type,
Account_Holder_Tittle = null,
Account_Holder_FirstName =null,
Account_Holder_LastName = null
}))
.Union(context.Current_Account_Holder_Details.Where(x=>x.Account_Number ==accountNumber).Select(d=> new AccountTransaction
{
Account_Number = d.Account_Number,
Account_Balance = null,
Deposit =null,
Withdrawal = null,
Date = null,
Account_Type = null,
Account_Holder_Tittle =d.Tittle,
Account_Holder_FirstName=d.Account_Holder_First_Name,
Account_Holder_LastName=d.Account_Holder_Last_Name
}));
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Serialize(inOut);
}
}
Below is the accompanying code:
@{
Layout = null;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $http, $window) {
$scope.IsVisible = false;
$scope.Search = function () {
var post = $http({
method: "GET",
url: "http://localhost:52098/HalifaxIISService.svc/AccountDetails/" + encodeURIComponent($scope.Account_Number),
dataType: 'json',
headers: {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/json; charset=utf-8'
}
});
post.then(function (response) { // .success(function(data => .then(function(response
var data = response.data; // extract data from resposne
$scope.Customers = JSON.parse(data); // eval(data.d) => JSON.parse(data)
$scope.IsVisible = true;
}, function (err) {
$window.alert(err);
});
}
$scope.grandTotal = function () {
return $scope.Customers.reduce(function (previousTotal, m) {
var valueToAdd = parseFloat(m.Deposit);
if (isNaN(valueToAdd))
return previousTotal;
return previousTotal + valueToAdd;
}, 0); // Send in 0 as the default previousTotal
}
$scope.grandTotal1 = function () {
return $scope.Customers.reduce(function (previousTotal, m) {
var valueToAdd = parseFloat(m.Withdrawal);
if (isNaN(valueToAdd))
return previousTotal;
return previousTotal + valueToAdd;
}, 0); // Send in 0 as the default previousTotal
}
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
Account Number:
<input type="text" ng-model="Account_Number" />
<input type="button" value="Submit" ng-click="Search()" />
<hr />
<br />
<div ng-repeat="m in Customers" ng-show="IsVisible">Account Type:{{m.Account_Type}}</div>
</div>
</body>
</html>
This screenshot displays the output of running the application: https://i.sstatic.net/mGUVN.png