I'm just starting out with angularjs (started today...) and I'm having trouble passing a model from my c# controller to an angularjs controller.
It appears that I need to call a get method in my angular controller to fetch data from the c# controller which will return json for me to load into $scope.people for further manipulation:
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
$http({url: "/Home/GetPersons",method: "GET"
}).success(function (data) {
$scope.people = data;
}).error(function (error) {
$scope.error = "Failed to load";
});
});
</script>
This is my controller :
[HttpGet]
public JsonResult GetPersons()
{
using (HRMEntities db = new HRMEntities())
{
var EmployeeList = db.Employees.Where(e => e.EmployeeId >= 0).ToList();
return Json(EmployeeList, JsonRequestBehavior.AllowGet);
}
}
When I make the request, I receive error code 500;
What am I doing wrong in the request? Is there an easier way to do this? Maybe utilizing the model passed to the view from the c# controller @model List<Employee>