如何从Angular Controller传递对象到ApiController

时间:2014-09-22 19:49:02

标签: c# asp.net-mvc angularjs asp.net-web-api2

我正在尝试将对象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 }
    );

1 个答案:

答案 0 :(得分:2)

$scope.EmailPdf = function () {
   id = $scope.currentItem.JobId
    $http.get('/api/Pdf/' + id).success(function () {
        $scope.PrintPreviewModal();
    });
}
这是一种方式。根据API的复杂程度,您还应该查看ngResource服务。