3

I am new to angularjs(started today...) and I have an issue with passing model from c# controller to a controller of angularjs.

It seems that I have to call a get method in my angular controller, to call a c# controller from which I will get json and load it to $scope.people for further manipulations:

<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>

Here 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);
        }
    }

As a response I get error code 500; What is wrong in the request? And could this be made in easier way? May be using my model which is sent to a view from c# controller @model List<Employee>

9
  • 2
    500 means server side error, so you need to debug your method to see what's wrong Commented Sep 30, 2016 at 11:38
  • I cant get how to debug it..EmployeeList is set, it has 67 elements inside, next step is already return, after which error is shown. Commented Sep 30, 2016 at 11:44
  • 2
    my best guess is that your EmployeeList contains a circular reference of some kind, so it fails to serialize it. do like this: var myJson = Json(EmployeeList, JsonRequestBehavior.AllowGet); return myJson Does it succeed to create the myJson var? Commented Sep 30, 2016 at 11:53
  • Creation succeed, myJson.data has 67 elements, but after return myJson there still is an error. Commented Sep 30, 2016 at 11:57
  • I tend to agree with Joey. Are there any virtual "lazy loaded" properties on your Employee model? If so, you may need to create a DTO or add the [ScriptIgnore] attribute to the virtual properties. Commented Sep 30, 2016 at 11:59

2 Answers 2

1

So, after some discussions, the solution to the problem seems to be this:

return Json(EmployeeList.Select(x=>new {Name=x.Name }), JsonRequestBehavior.AllowGet)

Or in other words, creating a Data Transfer Object (DTO) and populating it form EmployeeList

Perhaps there were problems with JSON deserialization.

Glad to help you!

Sign up to request clarification or add additional context in comments.

Comments

0

Im a little late to this one, but we created this at our company and it is VERY handy to have models that correlate from c# to js (vanilla) and we have angularjs factory model wrappers to extend Domain Model functionality.

https://github.com/castle-it/sharp2Js

Comments

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.