Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm using AngularJS with MVC4. When I click on a row inside data-ng-grid, I want it to call the GET method from my Web API. BUT... I want it to call the GET BY ID method. For some reason it always calls the GET method that retrieves all the Movies. How do I call the GET method that uses the param id? not sure what im doing wrong.

var movieResource = $resource('/api/movie', {}, { update: { method: 'PUT' }, getById: { method: 'GET', params: 'id' } });

    $scope.$watchCollection('selectedMovies', function () {
        //$scope.selectedMovie = angular.copy($scope.selectedMovies[0]);
        movieResource.getById(5);
    });


    public MovieData Get(int id)
    {
        var movie = movieRepository.GetDataById(id);
        if (movie == null)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }

        return movie;
    }

MovieController

    // GET api/movie
    public IEnumerable<MovieData> Get()
    {
        return movieRepository.GetAllData().AsEnumerable();
    }

    // GET api/movie/5
    public MovieData Get(int id)
    {
        var movie = movieRepository.GetDataById(id);
        if (movie == null)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }

        return movie;
    }
share|improve this question

1 Answer 1

up vote 1 down vote accepted

According to $resource documentation you have to provide params like this: movieResource.get({id: 5})

Also you can use $http service $http.get('/api/movie/'+id)

share|improve this answer
    
Thank you. movieResource.get({id: 5}) was correct! – FaNIX Apr 8 '14 at 12:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.