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());
}
}
あなたは 'window.location.reloadを()しないだろう;'とサーバーを得ればちょうどあなたのデータモデルからエンティティを削除します:そして、あなたは、コントローラとそれの表示を更新するために、成功コールバックを追加することができます確認 – charlietfl
まず、 '成功'と 'エラー'は推奨されません。代わりに 'then'と' catch'を使います。第2に、 'window'を使うのは悪い習慣です。 Angularは必要なものをすべて読み込みます。データ( '$ scope'の変数)を変更するだけで、ビューは自動的に更新されます。 – dror
ちょうど頭が上がって、角度は双方向の結合を使います。つまり、モデルまたはビューのいずれかの変更は、反対側に反映されます。 –