如何将对象从Angular Controller传递到ApiController

本文关键字:ApiController Controller Angular 对象 | 更新日期: 2023-09-27 18:23:54

我正试图将对象Id传递给ApiController。目前没有通过任何法案。也没有错误。现在,当我特别在Get Call中放入JobId时,一切都很好,所以我一直在思考如何让它与任何JobId一起工作。

查看

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

如何将对象从Angular Controller传递到ApiController

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

这是一种方式。根据API的复杂性,您还应该研究ngResource服务。