2016-06-15 18 views
1

AngularとMVCの新機能です。自分のビューからmvcコントローラにパラメータを渡そうとしています。レコードを削除した後で、変更を確認するためにビューを更新する必要があります。 ご協力いただきありがとうございます。サービスからMVCコントローラへのパラメータを角度で渡します。

myApp.service("deleteService", function ($rootScope, $http) 
{ 
    this.removeRow = function (recId) { 

     $http.delete("CurrentSettingsController.cs/DeleteRecord", { params: { recordId: recId } }) 
     .success(function (data, status, headers, config) { 
      window.location.reload(); 
     }) 
     .error(function (data, status, header, config) { 
     }); 
    } 
}); 

myApp.controller('myController', ['$scope', 'companiesService', 'allCurrentSettingsService','deleteService', 
    function ($scope, companiesService, allCurrentSettingsService, deleteService) { 
     $scope.currentSettings = ''; 
     companiesService.getList().then(function (value) { 
      $scope.currentSettings = value; 

     }), 
     $scope.allSettings = ''; 
     allCurrentSettingsService.getList().then(function (value) { 
      $scope.allSettings = value; 

     }), 
     $scope.deleteRecordFromDB = function (recId) { 
      deleteService.removeRow(recId); 
     }; 
    } 
]); 

[HttpPost] 
public static void DeleteRecord(int settingID) 
{ 
    try 
    { 
     using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      conn.Open(); 
      using (SqlCommand command = new SqlCommand("DeleteCurrentRecord", conn)) 
      { 
       command.CommandType = System.Data.CommandType.StoredProcedure; 
       command.Parameters.Add("@SettingId", SqlDbType.VarChar).Value = settingID; 
       command.ExecuteNonQuery(); 
       command.Parameters.Clear(); 
      } 
      conn.Close(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.Write(ex.ToString()); 
    } 
} 
+0

あなたは 'window.location.reloadを()しないだろう;'とサーバーを得ればちょうどあなたのデータモデルからエンティティを削除します:そして、あなたは、コントローラとそれの表示を更新するために、成功コールバックを追加することができます確認 – charlietfl

+1

まず、 '成功'と 'エラー'は推奨されません。代わりに 'then'と' catch'を使います。第2に、 'window'を使うのは悪い習慣です。 Angularは必要なものをすべて読み込みます。データ( '$ scope'の変数)を変更するだけで、ビューは自動的に更新されます。 – dror

+0

ちょうど頭が上がって、角度は双方向の結合を使います。つまり、モデルまたはビューのいずれかの変更は、反対側に反映されます。 –

答えて

0

サービスから$ http.deleteを返すことができるので、約束が返されます。通常

myApp.service("deleteService", function ($rootScope, $http) 
{ 
    this.removeRow = function (recId) { 

     return $http.delete("CurrentSettingsController.cs/DeleteRecord", { params: { recordId: recId } }); 
    } 
}); 

myApp.controller('myController', ['$scope', 'companiesService', 'allCurrentSettingsService','deleteService', 
    function ($scope, companiesService, allCurrentSettingsService, deleteService) { 
     $scope.currentSettings = ''; 
     companiesService.getList().then(function (value) { 
      $scope.currentSettings = value; 

     }), 
     $scope.allSettings = ''; 
     allCurrentSettingsService.getList().then(function (value) { 
      $scope.allSettings = value; 

     }), 
     $scope.deleteRecordFromDB = function (recId) { 
      deleteService.removeRow(recId).then(
       function(success) { 
        //do refresh here 
       }, 
       function(error) { 
        //error callback 
       } 
      ); 
     }; 
    } 
]); 
+2

はまだページをリロードします – charlietfl

+0

ありがとうございます。どうすればMVCコントローラから値を取得し、それを私の "DeleteRecord"メソッドに渡すことができます – user6440175

+0

ありがとうcharlietfl、答えを変更しました –

関連する問題