texas697
texas697

Reputation: 6387

How to pass object to ApiController from Angular Controller

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

Upvotes: 0

Views: 1077

Answers (1)

Paul
Paul

Reputation: 36319

$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.

Upvotes: 2

Related Questions