1

JsonResult method not calling through $http call,

I am working on a project that uses ASP.NET MVC, AngularJS I am calling a mvc controller from AngularJS. I am getting an jsonresult as in the call to a MVC controller from AngularJS .

this is the result

[
  {
    "Branch_ID": 1,
    "Branch_Name": "sdsds",
    "Branch_Address": "sfsdfsdf",
    "Branch_email": "sdfsdfsdf",
    "Branch_Notes": "sfsffsfd",
    "Branch_Manager": null,
    "Branch_Phone": null,
    "Branch_TimeFrom": "/Date(-2208996000000)/",
    "Branch_TimeTo": "/Date(-2208996000000)/",
    "saturday": false,
    "sunday": false,
    "monday": false,
    "tuesday": false,
    "wednesday": false,
    "thursday": false,
    "friday": false,
    "Departments": null
  }
]

branches controller

public class BranchesController : Controller
   {

    private IRepositoryBase<Branches> BrancheRepository;

    public BranchesController(IRepositoryBase<Branches> brancheRepository)
    {
        this.BrancheRepository = brancheRepository;
    }
    // GET: Branches
    public JsonResult Index()
    {

        var branches =   BrancheRepository.GetAll();

        //if (!String.IsNullOrEmpty(searchString))
        //{
        //    branches = branches.Where(s => s.Branch_Name.ToUpper().Contains(searchString.ToUpper()));
        //}

        return Json(branches, JsonRequestBehavior.AllowGet);
    } 
}

Index.cshtml

<div class="container" ng-controller="branch-controller">
<div class="panel panel-info">
    <div class="panel-heading">
        Branch Details - Grid CRUD operations
    </div>
    <table class="table table-bordered">
        <thead style="background-color:lightblue;">
            <tr>
                <th> Branch Address</th>
                <th> Branch Email</th>
                <th>Branch Name</th>
                <th>Branch Notes</th>
                <th> Actions</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="branche in Branches">
                  <td>{{branche.Branch_ID}}</td>
                <td>{{branche.Branch_Address}}</td>
                <td>{{branche.Branch_email}}</td>
                <td>{{branche.Branch_Name}}</td>
                <td>{{branche.Branch_Notes}}</td>

                <td style="width:200px;">
                    <a href="#" class="btn btn-info">Update</a>
                    <a href="#" class="btn btn-danger">Delete</a>
                </td>
            </tr>
        </tbody>
    </table>
</div>

Module.js

 var myapp;
 (function () {

   myapp = angular.module('my-branches', []);
  })();

Controller.js

myapp.controller('branch-controller', function ($scope, branchService) {

//Loads all branch records when page loads
loadBranches();

function loadBranches() {
    var BrancheRecords = branchService.getAllBranches();

    BrancheRecords.then(function (data) {
        //success

        $scope.Branches = data;
    },
    function (error) {
        console.log(error);
        alert("Error occured while fetching branche list...");
    });
    }
  });

Service.js

myapp.service('branchService', function ($http) {

    this.getAllBranches = function () {

        return $http.get("/Branches/Index").then(function (response) {

            return response.data;
        });
    };
});   
2
  • What's the issue? You cannot hit action, or you get response but cannot display it? Commented Dec 15, 2019 at 20:33
  • @Alexander i get response but i cannot display it, it is display like this [{"Branch_ID":1,"Branch_Name":"sdsds","Branch_Address":"sfsdfsdf","Branch_email":"sdfsdfsdf","Branch_Notes":"sfsffsfd","Branch_Manager":null,"Branch_Phone":null,"Branch_TimeFrom":"/Date(-2208996000000)/","Branch_TimeTo":"/Date(-2208996000000)/","saturday":false,"sunday":false,"monday":false,"tuesday":false,"wednesday":false,"thursday":false,"friday":false,"Departments":null}] Commented Dec 15, 2019 at 20:39

2 Answers 2

1

First of all you need to change you code as it is shown in @georgeawg's answer and then your remaining problem is that you are using invalid property names. The result should look like this

<td>{{branche.Branch_Address}}</td>
<td>{{branche.Branch_email}}</td>
<td>{{branche.Branch_Name}}</td>
<td>{{branche.Branch_Notes}}</td>
Sign up to request clarification or add additional context in comments.

7 Comments

Still have the same problem after changing the code with property names
i am still have the same problem please advice
@hashim Have you tried to debug your js code? Add debugger keyword before this line return response.data; and inspect what data response contains
VM246:1 Uncaught ReferenceError: response is not defined i have this error
@hashim Please update your questions with your current code, I'll try to figure out the problem
|
0

Change the service call to:

var BrancheRecords = branchService.getAllBranches();

BrancheRecords.then(function (data) {
    //success
    $scope.Branches = data;
},
  function (error) {
    console.log(error);
    alert("Error occured while fetching branche list...");
});

Change the service to:

myapp.service('branchService', function ($http) {

    this.getAllBranches = function () {

        return $http.get("Branches/Index").then(function(response) {
            return response.data;
        });
    };
});

The data is assigned to the data property of the response object.

For more information, see

3 Comments

Still have the problem after change my code to your code
i am still have the same problem please advice
i didn't fixed this bug please advice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.