-2

I am trying to implementing ajax call on a controller to get the list of all the students but i am not getting it on a button click I am putting my code here please anyone help

  module.controller("GetController", function ($scope, $http) {
    $scope.getUser = function () {
        console.log(getUser);
            var getdata=  $.ajax({
            type: 'POST',
            url: "/GetUserDetails",
            dataType: "json",
            contentType: "application/json",
            data: '{}',
            success: function (data) {
                var parsed = $.parseJSON(data.d);
                $.each(parsed, function (i, jsondata) {
                    $("#showdata").append("ID: " + jsondata.Id + "<br/>" + "FirstName: " + jsondata.FirstName + "<br/>" + "SecondName: " + jsondata.SecondName + "<br/>" + "EmailId: " + jsondata.EmailId + "<br/>" + "Mobile: " + jsondata.Mobile);
                });
            },
            error: function (XHR, errStatus, errorThrown) {
                var err = JSON.parse(XHR.responseText);
                errorMessage = err.Message;
                console.log(errorMessage);
            }
        });
    }
});

Here is the html

 <div ng-controller="GetController" id="showdata">
   <table>
       <tr>
           <td>Id</td>
           <td>FirstName</td>
           <td>SecondName</td>
           <td>EmailId</td>
           <td>Mobile</td>
       </tr>
       <tr>
           <td>{{Id}}</td>
           <td>{{FirstName}}</td>
           <td>{{SecondName}}</td>
           <td>{{EmailId}}</td>
           <td>{{Mobile}}</td>
       </tr>
   </table>       
   <input type="submit" ng-click="getUser()" value="Getdata" />

and here is the controller code

 [HttpPost]
    [Route("GetUserDetails")]
    public async Task<bool> GetUserDetails([FromBody]object obj)
    {
        return await CenterGateWay.GetStudentlist();

    }
Gaurav_0093
  • 1,040
  • 6
  • 28
  • 56

1 Answers1

2

You misuse the combination of jquery and angular. Don't ever do DOM manipulation in your controller, instead let angular handle it. Something like this should work:

$scope.users = [];

$scope.getUser = function () {
    $.ajax({
        type: 'POST',
        url: "/GetUserDetails",
        dataType: "json",
        contentType: "application/json",
        data: '{}',
        success: function (data) {
            // you have to trigger the digest cycle here,
            // because you use jquery's ajax which won't automatically be
            // picked up by angular
            $scope.$apply(function(){
                $scope.users = angular.fromJson(data.d);
            });
        },
        error: function (XHR, errStatus, errorThrown) {
            var err = angular.fromJson(XHR.responseText);
            errorMessage = err.Message;
            console.log(errorMessage);
        }
    });
}

And in your view:

<table>
    <tr>
        <td>Id</td>
        <td>FirstName</td>
        <td>SecondName</td>
        <td>EmailId</td>
        <td>Mobile</td>
    </tr>
    <tr ng-repeat="user in users">
        <td>{{ user.Id }}</td>
        <td>{{ user.FirstName }}</td>
        <td>{{ user.SecondName }}</td>
        <td>{{ user.EmailId }}</td>
        <td>{{ user.Mobile }}</td>
    </tr>
</table>

Even better, there is no need to use jquery at all:

$scope.getUser = function () {
    $http.post("/GetUserDetails").then(function(data) {
        $scope.users = data.d;
    }, function(error) {

    });
}
devqon
  • 13,818
  • 2
  • 30
  • 45