Dona Dona - 5 months ago 13
AngularJS Question

Connection between AngularJS and Asp.Net MVC

I'm new using angular in MVC, I'm tring to read data from my controller make with ASP.NET MVC EF, to angular.
I'm try with this:

I added in my MVC Controller (the path is '/Controllers/BrandController.cs') this code:

public JsonResult GetBrands()
{
var result = db.Brands.ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}


And then in the file js I put both service and controller of angular, that is the code:

var BrandApp = angular.module('BrandApp', []);

//Controller
BrandApp.controller('BrandController', function ($scope, BrandService) {

getBrands();
function getBrands() {
BrandService.getBrands()
.success(function (items) {
$scope.brands = items;
console.log($scope.brands);
})
.error(function (error) {
$scope.status = 'Unable to load customer data: ' + error.message;
console.log($scope.status);
});
}

});

//service
BrandApp.factory('BrandService', ['$http', function ($http) {

var BrandService = {};
BrandService.getBrands = function () {
return $http.get('/Home/GetBrands');
};
return BrandService;

}]);


What I'm wrong?

Thanks for you answers!!!

Win Win
Answer

Since the controller name is BrandController, URL should be /Brand/GetBrands.

BrandService.getBrands = function () {
   return $http.get('/Brand/GetBrands');
};

Normally, when you dealing with that kind of problem, you want to debug using Fiddler, Chrome Plugin PostMan, or some other tools.