使用ASP.Net MVC和Angular JS删除操作

时间:2018-01-18 05:36:20

标签: angularjs asp.net-mvc

我是ASP.Net MVC和Angular JS的新手。我正在使用这两种技术执行删除操作,但我在执行这些操作时收到404:找不到页面错误。 我的控制器代码:

//Delete Branch Data
$scope.deleteBranchData = function (dataId) {
    $http.delete('/Master/DeleteBranchMaster?branchId=' + dataId);
};

Angular Code:

nextInt()

错误: enter image description here

2 个答案:

答案 0 :(得分:1)

  // Delete action...
    [HttpPost]
    public string DeleteBranchMaster(string fsBranchId)
    {
         //Add your delete operation logic here... 
         //You can return success/fail string message

            return "Record deleted successfully.";
    }


//add following script...    
var app = angular.module("mvcCRUDApp", []); 
app.controller("mvcCRUDCtrl", function ($scope, crudAJService) {

$scope.deleteBranchData = function (dataId) {
        var getBranchData = crudAJService.DeleteBranchService(dataId);
        getBranchData.then(function (msg) {
            alert(msg.data);            
        }, function () {
            alert('Error while delete operation..!! Try again after sometime..');
        });
    }
});

//this is angularjs service..Service name is crudAJService..
app.service("crudAJService", function ($http) {

    //Delete service.. It will call Master controller's DeleteBranchMaster action.
    this.DeleteBranchService = function (branchId) {
        var response = $http({
            method: "post",
            url: "Master/DeleteBranchMaster",
            params: {
                fsBranchId: JSON.stringify(branchId)
            }
        });
        return response;
    }
});

答案 1 :(得分:0)

更改控制器 - 按照显示的操作或使用路径名称正确定义ID名称

[HttpDelete]
public ActionResult DeleteBranchMaster(int id)
{
     //Deletion Logic.
}

//删除分支数据

$scope.deleteBranchData = function (dataId) {
  $http.delete('/Master/DeleteBranchMaster/' + dataId);
};

或者

[HttpDelete("{branchId}")]
public ActionResult DeleteBranchMaster(int branchId)
{
     //Deletion Logic.
}