0

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

1 Answer 1

1

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)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.