1

I want to send object to angularjs Controller using MVC Controller Action is it possible?

suppose

 public ActionResult Dashboard()
    {
        return View();
    }

I want to pass object to app.js how to do this

3
  • Be more specific. Also post the code. Commented Oct 15, 2015 at 10:44
  • By MVC Controller Action you mean from Spring controller? Commented Oct 15, 2015 at 10:51
  • no from Asp.net MVC controller action means in return view() i want to pass data to angularjs controller i want know is it possible or not Commented Oct 15, 2015 at 11:01

1 Answer 1

2

Your question is a bit vague , you need to be more specific on what exactly you are trying to do.

Generally , this his how you would get data in Angular from the MVC application.

In Case of MVC/WebAPI , you should use actions to return JSON result back to the angular service which can then be processed by angular. Example below :

  app.factory('myService', function($http) {
  var myService = {
    GetData: function() {
      // $http returns a promise, which has a then function, which also returns a promise
      var promise = $http.get('<ActionURL>').then(function (response) {
        // The then function here is an opportunity to modify the response
        console.log(response);
        // The return value gets picked up by the then in the controller.
        return response.data;
      });
      // Return the promise to the controller
      return promise;
    }
  };
  return myService;
});

app.controller('MainCtrl', function( myService,$scope) {
  // Call the async method and then do stuff with what is returned inside our own then function
  myService.GetData().then(function(d) {
    $scope.data = d;
  });
});

After this services is called from the MainCtrl , angular will have the data from the MVC action available in its $scope.data variable.

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

2 Comments

This is the preferred approach. It's also possible to deserialize a model to json which saves a trip to the server
@JeffDunlop : How do you mean ? Cant visualize what you mean from the comment . Got a sample ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.