2014-09-22 33 views
0

我想將對象ID傳遞給ApiController。目前沒有任何事情可以通過。也沒有錯誤。現在,當我在Get Call中特別放置一個JobId時,所有工作都正常,所以我被困在如何使它與任何JobId一起工作。如何從Angular Controller將對象傳遞給ApiController

查看

<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" /> 

控制器

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

路由

config.Routes.MapHttpRoute(
     name: "PdfApi", 
     routeTemplate: "api/{controller}/{id}", 
    defaults: new { controller = "apiGeneratePdf", id = RouteParameter.Optional } 
    ); 
+0

你沒有把id傳給你的ajax調用。 – 2014-09-22 19:55:05

回答

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

這是一種方法。根據您的API的複雜程度,您還應該查看ngResource服務。

+0

工作,謝謝。我想過切換到ngResource。 – texas697 2014-09-22 19:58:00

+1

是的,當你用$ http重寫相同的4次crud操作幾次$資源變得有吸引力和IMO值得(也使得它,所以我肯定會在後端編寫一致的RESTful位)。 – shaunhusain 2014-09-22 19:59:36

相關問題