0

Edited on 10/05

I've been following this anjularjs and asp.net tutorial. I am trying to add some additional things on my own, but I am stuck. I am getting a JSon object from the server, and I am trying to display the object's content to the client. The issue is that I am getting [object Object] on the client side when I am expected to see a list of ID, Name, and Password. What am I missing here?

//The controller

  myApp.controller('userController', function ($scope,$http /*UserService*/) {
      //  $scope.Users = [];
        $http.post('/Templates/ListUsers',{ id : 0})
        .success(function (data) {
            // $scope.Users = data.data;
            if (data.Ok) {
                $scope.Users = data.data;
                console.log($scope.Users);
            }
        }).error(function(error){
            console.log(error);
        });

//The form where the data is supposed to display. 
<div class="row">
       <div class="form-group">
           <li ng-repeat="x in Users"> 
**//I am iterating through the users, 
//so I was not expected to see [object Object]**
               {{ x.ID, x.Name, x.Password  }}
           </li>
       </div>
   </div>   

//The JSON object in the console window:
[{"ID":1,"Name":"Name1","Password":"Password1"},
{"ID":2,"Name":"Name2","Password":"Password2"},     
{"ID":3,"Name":"Name3","Password":"Password3"},
{"ID":4,"Name":"Name4","Password":"Password4"}]

The index page with AngularJS and Angular-Route Version 1.4.7

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
  <head>
    <title>AngularJS Routing example</title>
      <base href="/">
  </head>

  <body>

    <div class="container">
        <div class="row">
        <div class="col-md-3">
            <ul class="nav">
                <li><a href="/NewOrder"> Add New Order </a></li>
                <li><a href="/ShowOrders/123"> Show Order </a></li>
               <div ng-controller="CalculatorController">
                <li><a href="/Calculator">Calculator</a></li>
              </div>
                <li><a href="/Users">Add Users</a></li>
            </ul>
        </div>
        <div class="col-md-9">
            <div ng-view></div>
        </div>
        </div>
    </div>


@*   <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.js"></script>
   <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular-route.js"></script>  *@
    <script src="~/Scripts/angular.js"></script>
    <script src="~/Scripts/angular-route.js"></script>
   <link rel="stylesheet" href="style.css" />
    <script src="~/Scripts/myApp.js"></script>
  </body>

//Updated on 10/06:

Instead of using CDN references, I downloaded AngularJS and AngularJS-Route 1.4.7 and referenced them instead. When I ran the application, it simply display the entire JSON object.

[HttpGet]
    public JsonResult ListUsers()
     {
       return View();
     }

 [HttpPost]
        public JsonResult ListUsers(int id=0)
        {
            try
            {
                List<User> listUser = new List<User>()
            {
                new User{ ID= 1, Name = "Name1", Password="Password1"},
                new User{ ID= 2, Name = "Name2", Password="Password2"},
                new User{ ID= 3, Name = "Name3", Password="Password3"},
                new User{ ID= 4, Name = "Name4", Password="Password4"}
            };

                return Json(new { Ok = true, data = listUser, message = "Success" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                ex.ToString();
                return Json(new { Ok = false, data = "", message = "Failure" }, JsonRequestBehavior.AllowGet);
            }
        }

The Route Added

  routes.MapRoute(
                name : "listUsers",
                url : "Templates/ListUsers/{id}",
                defaults : new { controller = "Templates", action = 
 "ListUsers", id = UrlParameter.Optional });

enter image description here

Updated on 10/07

I noticed that changing the view to return a view and hard coding the json object to the users' controller works. Because of that, I added another method with a [HttpPost] annotation, changed the $http.get to $http.post, and added the ID parameter. Also, I modified the route to have an optional id so that '/Templates/ListUsers' will work so it '/Templates/ListUsers/1'. I was expected $http.post with a parameter of 1 to redirect to method with the HttpPost annotation, but it did not happen. I am not sure that things are making sense to me at this point. I am reading the $http.post again and see if I can see where I made the mistake.

2 Answers 2

1

Your base template should have a ui-view to hold the preLogin partial view, else angular won't know where to put it. You can even write the template itself on the base template, or make it a directive, and then show/hide based on certain criteria in your base controller.

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

9 Comments

I thought I was telling Angular to put it in container one in the configuration file. Am I missing something? .state('login', { ... "containerOne": { templateUrl: '/Home/PreLogin', controller: preLoginController }, "containerTwo": { templateUrl: '/Account/Login', controller: LoginController } } <div class="row"> <div class="col-md-6"> <div ui-view="containerOne"></div> </div>
Isn't /Home/PreLogin return JSON data instead of the template? Can you make a fiddle if this can't solve your problem? There is too much code here.
I updated and changed part of the text. I am getting and viewing the data in console.log, but I am not able to display it to the user. It only display [object Object] instead of ID, Name, and Password. Since I am using ng-repeat, I wan't expected to see [object Object]
Can you try separating the variables? {{ x.ID }}, {{ x.Name }}, {{ x.Password }}
I changed it {{ x.ID }}, {{ x.Name }}, {{ x.Password }}, and it did not work. Also, I downloaded the latest version of AngularJS and AngularRoute v1.47 and referenced that instead of the using CDN reference. I ran the application, and it simply displayed the entire json object that was returned instead of the ID, Name, and Password.
|
0

Finally, I got it to work although another issue pops its head up. I removed the default route and added a catch all route.

 routes.MapRoute(
                name : "catchAll",
                url : "{*Url}",
                defaults : new { controller = "Templates", action = 
 "ListUsers", id = UrlParameter.Optional });

By coincidence, I also remove the comment for the route below

 routes.MapRoute(
                name : "Default",
                url : "{controller}/{action}/{id}",
                defaults : new { controller = "Home", action = 
 "Index", id = UrlParameter.Optional });

I did a post, and it worked. If I commented the catch all route, it worked fine. However, the catch all route did not appear to be doing anything at this point because refreshing the page returned 404. If I moved the catch all above the default route, it stopped working too. I still have to figure this part out now.

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.