2014-02-17 82 views
0

在我的Angular/WebAPI應用程序中,我努力通過其ID檢索特定記錄。 在前端我有一個控制器和一個數據服務。控制器調用數據服務的方法,數據服務將$ http調用給WebAPI。 在我的控制器中,我將所需記錄的OID傳遞給數據服務的getServiceRequestById方法。我的問題之一是,這個OID的實際價值是:1而不是1。 我的另一個問題是,當數據服務打電話給WebAPI時,WebAPI察覺到請求,好像它齲沒有ID,並將該請求傳遞給它的Get()方法,而不是Get(int Id)。 這裏是我的前端控制器:如何通過ID正確檢索記錄

angular.module('frontEndApp').controller('EditServiceRequestCtrl',['$scope', 'requestsRepository','$routeParams', 
    function ($scope, requestsRepository,$routeParams) { 
     console.log("This is EditServiceRequestCtrl ; $routeParams: " + $routeParams); 
     //First we make a call to the data service, to fetch our ServiceRequest by its OID 
     //Then, in the callback function, we populate the $scope models below with the data of our retreived ServiceRequest 
     var getCleanId = function() { 
      return $routeParams.OID.substring(0, 2) 
     }; 
     var Id = getCleanId(); 
     //var cleanId = id.substring(0, 2); 
     console.log('getCleanId Id: ' + Id); 
     requestsRepository.getServiceRequestById(Id, function (request) { 

      $scope.OID = request.OID; 
      $scope.RequestorName = request.RequestorName; 
      $scope.RequestorBusinessUnit = request.RequestorBusinessUnit; 
      $scope.CustomerName = request.CustomerName; 
      $scope.CscContactPerson = request.CscContactPerson; 
      $scope.IsWbsCodeAvailable = request.IsWbsCodeAvailable; 
      $scope.SalesforceIdNumber = request.SalesforceIdNumber; 
      $scope.ProjectCtv = request.ProjectCtv; 
      $scope.RequestedCompletionDate = request.RequestedCompletionDate; 
      $scope.ToBeUsedForCloudMigration = request.ToBeUsedForCloudMigration; 
      $scope.ToBeUsedForDatacenterMove = request.ToBeUsedForDatacenterMove; 
      $scope.ToBeUsedForServerRefresh = request.toBeUsedForServerRefresh; 
      $scope.DataRequirements = request.DataRequirements; 
      $scope.DataProtectionRequirements = request.DataProtectionRequirements; 
      $scope.ProjectedDataAvailability = request.ProjectedDataAvailability; 
      $scope.DiscoveryLeadName = request.DiscoveryLeadName; 
      $scope.SelectedCountries = request.SelectedCountries; 
      $scope.ManualDiscovery = request.ManualDiscovery; 
      $scope.AutomatedDiscovery = request.AutomatedDiscovery; 
      $scope.DataLoadUsingMasterTemplate = request.DataLoadUsingMasterTemplate; 
      $scope.DataLoadUsingAutomatedInterface = request.DataLoadUsingAutomatedInterface; 
      $scope.DataLoaderRequiresSitizenship = request.DataLoaderRequiresSitizenship; 

      $scope.countries = [ 
       { 
        name: "US", checked: false 
       }, 
       { 
        name: "UK", checked: false 
       }, 
       { 
        name: "France", checked: false 
       }, 
       { 
        name: "Germany", checked: false 
       }, 
       { 
        name: "Sweden", checked: false 
       }, 
       { 
        name: "Danmark", checked: false 
       } 
      ]; 
      var list = []; 

      $scope.checkit = function() { 

       for (var p in $scope.countries) { 
        if ($scope.countries[p].checked) { 
         list.push($scope.countries[p].name); 

         console.log("selected country: " + $scope.countries[p].name + " " + $scope.ProjectedDataAvailability); 
        } 
       } return list; 
      } 
      console.log('EditServiceRequestCtrl $scope.RequestorName : ' + $scope.RequestorName); 
     }); 
    $scope.updateServiceRequest = function() { 
     var ServiceRequest = { 
      requestorName: $scope.RequestorName, 
      requestorBusinessUnit: $scope.RequestorBusinessUnit, 
      customerName: $scope.CustomerName, 
      cscContactPerson: $scope.CscContactPerson, 
      isWbsCodeAvailable: $scope.IsWbsCodeAvailable, 
      salesforceIdNumber: $scope.SalesforceIdNumber, 
      projectCtv: $scope.ProjectCtv, 
      requestedCompletionDate: $scope.RequestedCompletionDate, 
      projectedDataAvailability: $scope.ProjectedDataAvailability, 
      toBeUsedForCloudMigration: $scope.ToBeUsedForCloudMigration, 
      toBeUsedForDatacenterMove: $scope.ToBeUsedForDatacenterMove, 
      toBeUsedForServerRefresh: $scope.ToBeUsedForServerRefresh, 
      dataRequirements: $scope.DataRequirements, 
      dataProtectionRequirements: $scope.DataProtectionRequirements, 
      selectedCountries: 
       list.filter(function (itm, i, a) { 
        return i == a.indexOf(itm); 
       }).toString(), 
      projectedDataAvailability: $scope.ProjectedDataAvailability, 
      discoveryLeadName: $scope.DiscoveryLeadName, 
      manualDiscovery: $scope.ManualDiscovery, 
      automatedDiscovery: $scope.AutomatedDiscovery, 
      dataLoadUsingMasterTemplate: $scope.DataLoadUsingMasterTemplate, 
      dataLoadUsingAutomatedInterface: $scope.DataLoadUsingAutomatedInterface, 
      dataLoaderRequiresSitizenship: $scope.DataLoaderRequiresSitizenship 

     }; 

     requestsRepository.updateServiceRequest(ServiceRequest); 
    } 
}]); 

這裏是我的前端數據服務:

frontEndApp.factory('requestsRepository',function ($http) { 

    var createServiceRequest = function (ServiceRequest) { 
     $http(
     { 
      url: 'http://localhost:8080/api/ServiceRequests', method: "POST", data: ServiceRequest, 
      headers: { 
       'Content-Type': 'application/json' 
      } 
     }).success(function (data, status, headers, config) { 
      console.log("createServiceRequest Status: " + status); 
     }).error(function (data, status, headers, config) { 
      console.log("createServiceRequest FAILURE: " + status + " ServiceRequest: " + ServiceRequest); 
     }); 
    }; 

    var updateServiceRequest = function (ServiceRequest) { 
     $http(
     { 
      url: 'http://localhost:8080/api/ServiceRequests', method: "PUT", data: ServiceRequest, 
      headers: { 
       'Content-Type': 'application/json' 
      } 
     }).success(function (data, status, headers, config) { 
      console.log("updateServiceRequest Status: " + status); 
     }).error(function (data, status, headers, config) { 
      console.log("updatetServiceRequest FAILURE: " + status + " ServiceRequest: " + ServiceRequest); 
     }); 
    }; 

    var getServiceRequests = function (successCallback) { 
     $http({ 
      method: 'GET', url: 'http://localhost:8080/api/ServiceRequests' 
     }).success(function (data, status, headers, config) { 
      successCallback(data); 
     }).error(function (data, status, headers, config) { 
      return status; 
     }); 
    }; 

    var getServiceRequestById = function (Id,successCallback) { 
     $http({ 
      method: 'GET', url: 'http://localhost:8080/api/ServiceRequests/' + Id 

     }).success(function (data, status, headers, config) { 
      console.log("getServiceRequestById, data: " + data); 
      successCallback(data); 
     }).error(function (data, status, headers, config) { 
      return status; 
     }); 
    }; 



    return { 
     createServiceRequest: createServiceRequest, getServiceRequests: getServiceRequests, 
     updateServiceRequest: updateServiceRequest, getServiceRequestById: getServiceRequestById 
    }; 
}); 

這裏是我的後端的WebAPI:

public HttpResponseMessage Get() 
     { 

      var requestList = from req in new XPQuery<DummyRequest>(uow) select req; 

      List<AccountViewServiceRequest> dataList = new List<AccountViewServiceRequest>(); 

      foreach(var item in requestList) 
      { 
       AccountViewServiceRequest sr = new AccountViewServiceRequest(); 
       sr.OID = item.Oid; 
       sr.RequestorName = item.RequestorName; 
       sr.RequestorBusinessUnit = item.RequestorBusinessUnit; 
       sr.CustomerName = item.CustomerName; 
       sr.CscContactPerson = item.CscContactPerson; 
       sr.IsWbsCodeAvailable = item.IsWbsCodeAvailable; 
       sr.SalesforceIdNumber = item.SalesforceIdNumber; 
       sr.ProjectCtv = item.ProjectCtv; 
       sr.RequestedCompletionDate = item.RequestedCompletionDate; 
       sr.ToBeUsedForCloudMigration = item.ToBeUsedForCloudMigration; 
       sr.ToBeUsedForDatacenterMove = item.ToBeUsedForDatacenterMove; 
       sr.ToBeUsedForServerRefresh = item.ToBeUsedForServerRefresh; 
       sr.DataRequirements = item.DataRequirements; 
       sr.SelectedCountries = item.SelectedCountries; 
       sr.DataProtectionRequirements = item.DataProtectionRequirements; 
       sr.ProjectedDataAvailability = item.ProjectedDataAvailability; 
       sr.DiscoveryLeadName = item.DiscoveryLeadName; 
       sr.ManualDiscovery = item.ManualDiscovery; 
       sr.AutomatedDiscovery = item.AutomatedDiscovery; 
       sr.DataLoadUsingMasterTemplate = item.DataLoadUsingMasterTemplate; 
       sr.DataLoadUsingAutomatedInterface = item.DataLoadUsingAutomatedInterface; 
       sr.DataLoaderRequiresSitizenship = item.DataLoaderRequiresSitizenship; 


       dataList.Add(sr); 
      } 
      var response = Request.CreateResponse(HttpStatusCode.OK, dataList.ToList()); 
      response.Headers.Add("Access-Control-Allow-Origin", "*"); 
      return response; 
     } 

     public HttpResponseMessage Get(int Oid) 
     { 

      var item = (from req in new XPQuery<DummyRequest>(uow) where req.Oid == Convert.ToInt32(Oid) select req).First(); 

       AccountViewServiceRequest sr = new AccountViewServiceRequest(); 
       sr.OID = item.Oid; 
       sr.RequestorName = item.RequestorName; 
       sr.RequestorBusinessUnit = item.RequestorBusinessUnit; 
       sr.CustomerName = item.CustomerName; 
       sr.CscContactPerson = item.CscContactPerson; 
       sr.IsWbsCodeAvailable = item.IsWbsCodeAvailable; 
       sr.SalesforceIdNumber = item.SalesforceIdNumber; 
       sr.ProjectCtv = item.ProjectCtv; 
       sr.RequestedCompletionDate = item.RequestedCompletionDate; 
       sr.ToBeUsedForCloudMigration = item.ToBeUsedForCloudMigration; 
       sr.ToBeUsedForDatacenterMove = item.ToBeUsedForDatacenterMove; 
       sr.ToBeUsedForServerRefresh = item.ToBeUsedForServerRefresh; 
       sr.DataRequirements = item.DataRequirements; 
       sr.SelectedCountries = item.SelectedCountries; 
       sr.DataProtectionRequirements = item.DataProtectionRequirements; 
       sr.ProjectedDataAvailability = item.ProjectedDataAvailability; 
       sr.DiscoveryLeadName = item.DiscoveryLeadName; 
       sr.ManualDiscovery = item.ManualDiscovery; 
       sr.AutomatedDiscovery = item.AutomatedDiscovery; 
       sr.DataLoadUsingMasterTemplate = item.DataLoadUsingMasterTemplate; 
       sr.DataLoadUsingAutomatedInterface = item.DataLoadUsingAutomatedInterface; 
       sr.DataLoaderRequiresSitizenship = item.DataLoaderRequiresSitizenship; 


      var response = Request.CreateResponse(HttpStatusCode.OK, sr); 
      response.Headers.Add("Access-Control-Allow-Origin", "*"); 
      return response; 
     } 

哪一部分我應該更正以成功檢索基於其OID的單個電子記錄嗎?

+0

沒有解決您的問題,但我認爲使用'ngResource'可能更適合您的應用程序。 – kubuntu

+0

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/creating-an-odata-endpoint如果我理解正確,您正在嘗試使用odata? –

回答

1

由於看來你的第一個問題是有關該程序:

var getCleanId = function() { 
     return $routeParams.OID.substring(0, 2) 
    }; 

更改起始位置爲1的子去除前置冒號。

var getCleanId = function() { 
     return $routeParams.OID.substring(1, 2) 
    }; 

這反過來應該解決的問題,沒有得到一個單一的記錄的web api。 Web API正試圖在Web服務器上的控制器中找到匹配的功能簽名。唯一的參數不能轉換爲整數,所以它使用Get()而不是Get(int Oid)。

+0

子字符串更正確實解決了預冒號冒號的問題,但由於某些原因,我的WebAPI仍未捕獲單個id的請求。我會發佈一個單獨的問題,並會在那裏顯示我的服務。 –