Md Hohn Md Hohn - 1 month ago 14
JSON Question

Why is angularJS not binding data?

Here I'm fetching maxim salary from Employee table.The JSON realize data properly but why angular not binding data
Controller.Js

app.controller('AdminCntrl', function ($scope, AdminServices) {
$scope.BtnSubmit = function () {
alert('ok1')
var Bust = AdminServices.Getmax();

Bust.then(function (d) {
$scope.Emp = d.data;

})
}
})


Service.Js

app.service('AdminServices', function ($http) {

this.Getmax = function () {
var ss = $http({
url: '/Department/Max',
method: 'Get',
data:JSON.stringify(),
content:{'content-type' : 'application/Json'}
})
return ss;
}
})


Controller

public JsonResult Max()
{
var xxx = db.Employees.OrderByDescending(ww => ww.Salary).First();
return new JsonResult { Data = xxx, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}


Emp.Cshtml

<div ng-controller="AdminCntrl">
<input type="button" class="btn btn-primary btn-sm" value="Min" ng-click="BtnSubmit()" />

<div ng-repeat="Accessor in Emp">
{{Accessor.Id}}
<span style="color:red">{{Accessor.Name}}</span>
</div>
</div>


Fiddler Catch Data

{"Id":11,"Name":"Saleem","Department":"Oracle","Salary":100000.00,"EmpType":"Admin","DataofJoin":"\/Date(1462127400000)\/","DataofBirth":"\/Date(597868200000)\/"}

Answer

Is the value of $scope.Empequal to your Fiddler Data?

It appears you're not returning an array which is what ng-repeat is expecting. I'd bet if your Fiddler data was represented as an array:

[{"Id":11,"Name":"Saleem","Department":"Oracle","Salary":100000.00,"EmpType":"Admin","DataofJoin":"\/Date(1462127400000)\/","DataofBirth":"\/Date(597868200000)\/"}] 

you'd see it bind.

Alternatively, if you're really not expecting an array then if you replace

<div ng-repeat="Accessor in Emp">
     {{Accessor.Id}}
     <span style="color:red">{{Accessor.Name}}</span>
</div>

with:

<div>
      {{Emp.Id}}
       <span style="color:red">{{Emp.Name}}</span>
</div>

You'd have some success