0

I am trying to pass a object Id to a ApiController. Right now nothing is being passed. There are no errors either. Now when I specifically put a JobId in the Get Call everything works fine so I am stuck on how to make it work with any JobId.

View

<div class="inline-fields">
<label>JobId:</label>
<input ng-model="currentItem.JobId" type="text">
 </div>
     <div class="inline-fields">
 <label>JobName:</label>
 <input ng-model="currentItem.JobName" type="text">
  </div>

 <input ng-click="EmailPdf(currentItem)" type="button" value="Email"  />

Controller

 $scope.EmailPdf = function () {
   id = $scope.currentItem.JobId
    $http.get('/api/Pdf/id').success(function () {
        $scope.PrintPreviewModal();
    });
}

ApiController

 // GET api/<controller>/5
    public string Get(int? id) // allow nullable parameter
    {
        if (!id.HasValue) // if null return an empty string
        {
            return string.Empty;
        }

        // your code :
        JobDataAdapter adapter = new JobDataAdapter();
        Job job = new Job();
        job = adapter.GetJob(id);
        if (job == null)
        {
            return string.Empty;
        }

Routing

config.Routes.MapHttpRoute(
        name: "PdfApi",
        routeTemplate: "api/{controller}/{id}",
    defaults: new { controller = "apiGeneratePdf", id = RouteParameter.Optional }
    );
1
  • You are not passing the id to yours ajax call. Commented Sep 22, 2014 at 19:55

1 Answer 1

2
$scope.EmailPdf = function () {
   id = $scope.currentItem.JobId
    $http.get('/api/Pdf/' + id).success(function () {
        $scope.PrintPreviewModal();
    });
}

that's one way. Depending on the complexity of your API you should also look into the ngResource service.

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

2 Comments

worked, thanks. I thought about switching to ngResource.
Yup after you rewrite the same 4 crud operations using $http a few times $resource becomes attractive and IMO worthwhile (also makes it so I am sure to write consistent RESTful bits on the back end).

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.