2015-02-10 128 views
0

我在我的ASP.net MVC應用程序中使用webapi和angularjs。一切工作正常,但在我的插入(後)方法中,我需要檢查一個值是否存在於數據庫中,然後再進行插入。我不確定應該去哪裏。我排除了webapi,因爲void不會返回值。邏輯上的地方似乎是控制器,但我找不到從插入控制器方法中調用角度getEmployee(id)方法的正確方法。任何幫助表示讚賞。ASP.Net MVC,WebAPI,AngularJS - 檢查數據庫中是否存在值

角控制器(後):

$scope.insertEmployee = function (employee) { 
    employeeFactory.insertEmployee(employee) 
     .success(function (emp) { 
      $scope.employee = emp; 
     }) 
     .error(function (error) { 
      $scope.status = 'Unable to load employee data: ' + error.message; 
     }); 
}; 

角廠(後):

factory.insertEmployee = function (employee) { 
    url = baseAddress + "employee/insert/"; 
    $http.post(url, employee).success(function (data) { 
     alert("Saved Successfully!!"); 
    }).error(function (data) { 
     $scope.error = "An Error has occured while saving employee! " + data; 
    }); 
}; 

的WebAPI控制器POST方法:

[Route("api/employee/insert/")] 
    public void Post(Employee employee) 
    { 
     if (ModelState.IsValid) 
     { 
      context.Employee.Add(employee); 
      context.SaveChanges(); 
     } 
    } 

角控制器(獲得):

$scope.getEmployees = function (term) { 
    employeeFactory.getEmployees(term) 
     .success(function (data) { 
      $scope.employees = data; 
     }) 
     .error(function (error) { 
      $scope.status = 'Unable to load employee data: ' + error.message; 
     }); 
}; 
+0

''scope''在'insertEmployee'裏面怎麼樣? – 2015-02-10 19:11:17

+0

我不知道。這是我在互聯網上找到的一個例子。 noob with angular – steveareeno 2015-02-11 16:59:56

回答

1

我會建議在你的web-api服務器端做這個。你可以構造一個異常並拋出它。一些僞代碼:

[Route("api/employee/insert/")] 
    public void Post(Employee employee) 
    { 
     if (ModelState.IsValid) 
     { 
      // verify doesn't already exist 
      if(...item-already-exists...) { 
       var resp = new HttpResponseMessage(HttpStatusCode.Conflict) 
       { 
        Content = new StringContent("Employee already exists!")), 
        ReasonPhrase = "Employee already exists!" 
       } 
       throw new HttpResponseException(resp); 
      } 

      context.Employee.Add(employee); 
      context.SaveChanges(); 
     } 
    } 
+0

謝謝。這是我想的,但有兩個控制器(MVC和Angular)扔我一個循環,特別是因爲webapi控制器方法沒有返回值 – steveareeno 2015-02-11 17:04:17

1

您的工廠與您的控制器不匹配。如果你想使用successerroremployeeFactory.insertEmployee,它需要返回承諾:

factory.insertEmployee = function (employee) { 
    url = baseAddress + "employee/insert/"; 
    return $http.post(url, employee); 
}; 

,那麼你可以做

employeeFactory.insertEmployee(employee).success(...) 

現在回答你的問題,你既可以做一個數據庫中讀取insertEmployee在插入之前檢查值是否存在。或者保存服務器調用並在插入請求期間執行檢查:如果該員工存在,則返回錯誤或特定消息以通知客戶端。

+0

謝謝。我會進行調整。 – steveareeno 2015-02-11 17:00:18

0

隨着floribon和尼古拉斯·史密斯的幫助下,我設法想出的氣泡誤差達到我的看法,我在一個div元素顯示的解決方案。這只是光禿禿的骨頭,而是一個開始。

我的角度控制器:

$scope.insertEmployee = function (employee) { 
    employeeFactory.insertEmployee(employee) 
     .success(function (data) { 
      $scope.employee = data; 
      $scope.status = 'The item was saved'; 
     }) 
     .error(function (error) { 
      $scope.status = 'Unable to save the employee data: ' + error; 
     }); 
}; 

我的角廠:

factory.insertEmployee = function (employee) { 
    url = baseAddress + "employee/insert/"; 
    return $http.post(url, employee); 
}; 

我的WebAPI控制器:

[Route("api/employee/insert/")] 
    public HttpResponseMessage Post(Employee employee) 
    { 
     HttpResponseMessage response; 

     // check for the employee 
     var employeeCheck = context.Employee 
         .Where(b => b.EmployeeNumber == employee.EmployeeNumber) 
         .FirstOrDefault(); 

     if (employeeCheck == null) 
     { 
      // save the item here 
      response = Request.CreateResponse(HttpStatusCode.OK); 
     } 
     else 
     { 
      response = Request.CreateResponse(HttpStatusCode.Conflict, "The item already exists"); 
     } 
     return response; 
    } 

其結果是,信息是「無法保存僱員數據:該項目已經存在「。