1

I am working on an asp.net mvc application and I am using Entity Framework and AngularJS in it. I am using AngularJS's $http service to call an action method and retrieve data from the server. The correct data is retrieved from the server (I confirmed this by debugging), but somehow an error occurs after the action method returns the retrieved data and the error callback function is fired instead of the success callback function. And then I get a status 500 in the browser's console.

Here are the involved blocks of codes:

(From angularjs controller)

    $http({
        url: rootUrl + "User/GetUser",//'@Url.Action("GetUser","User")',
        method: 'POST',
        params: {
            uname: $scope.username,
            pword: $scope.pass
        }
    }).then(function (response) {
        alert('success!');
        $scope.user = response.data;
        if ($scope.user.Fullname != undefined) {
            $http({
                url: rootUrl + "Session/Set",
                method: "POST",
                data: {
                    "key": "curr_user",
                    "value": JSON.stringify($scope.user)
                }
            });

            window.location.href = rootUrl + 'Product/List/';

        } else {
            //invalid login
            $("input[name='password']").select();
            $("#validation-summary").html("Wrong email or password.");
            $scope.invalidlogin = true;
            $(btnLogin).removeClass('disabled');
            $(btnLogin).text("Submit");

        }

(From mvc controller)

    [HttpPost]
    public JsonResult GetUser(string uname, string pword)
    {
        JBManager manager = null;

        using (SE_Context db = new SE_Context())
        {
            try
            {
                manager = db.Managers
                    .Include("Transactions.Items")
                    .Where(m => m.Username == uname && m.Password == pword)
                    .FirstOrDefault();
                //At this point, manager has the desired data
                return Json(manager, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

And here's a screenshot of the error in the browser: enter image description here

Would really appreciate any help. Thanks!

UPDATE: Everything was working fine before I used Entity Framework. (Just in case it has something to do with the issue)

12
  • 1
    First of all you need to change from HttpPost to HttpGet both in action header and in the ajax call and while returning from the action include JsonRequestBehaviour.AllowGet. Commented Dec 11, 2016 at 13:30
  • And how about handling the exception properly instead of "return null"? Commented Dec 11, 2016 at 13:34
  • @Rudresh Wouldn't changing the request type from POST to GET expose the username and password in the URL? Commented Dec 11, 2016 at 13:37
  • instead of returning null, return Json("") Commented Dec 11, 2016 at 13:37
  • @TigOldBitties How? Commented Dec 11, 2016 at 13:38

1 Answer 1

0

I think your issue is nested objects.You can flatten object graphs that contain nested objects using DTOs (Data Transfer Objects).

You can just try simple example as like below.If it'll work then you need to extend it to work with your EF query.

public class MyDto
    {
        public string Name { get; set; }
    }


    [HttpPost]
    public JsonResult GetUser(string uname, string pword)
    {
        JBManager manager = null;

        using (SE_Context db = new SE_Context())
        {
            try
            {
               //construct the DTO here
                manager = db.Managers.Select(a=> new MyDto(
                {
                 Name = a.Name
                 })).FirstOrDefault(m => m.Username == uname && m.Password == pword);

                return Json(manager, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

You can read more about DTOs here : Create Data Transfer Objects (DTOs)

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

3 Comments

I tried reading about DTOs but I can't seem to understand its relevance to my issue. Can you give a brief explanation of how DTOs can help or at least point me to a good article about DTOs? Thanks.
I have put a reference. please see that.
any feedback for this ?

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.