使用AngularJS调用异步控制器操作方法

时间:2015-06-05 12:13:38

标签: angularjs asynchronous

我是AngularJS的新手,我想知道如何通过angularJS在MVC中调用异步控制器操作方法。我已经尝试使用下面的代码。有人可以帮助我吗?这是我的AngularJS代码

 $scope.updateEmp = function () {
        var response = $http({
            method: "post",
            url: "/Home/UpdateCustomer",
            data: JSON.stringify($scope.Customer),
            dataType: "json"
        }).success(function () {
            $scope.cancel();
            toaster.pop('success', "Success", 'Updates Successfully...!!');
           // showAlert("alert-success", "Updated!");

        }).error(function () {
            toaster.pop('error', "Error", 'Error while getting data', null, 'trustedHtml');
           // alert("Error while getting data");
        });
        // return response;
    }

我的行动方法如下

  [HttpPost]
    public async void UpdateCustomer(Customer Upcustomer )
    {
        await System.Threading.Tasks.Task.Run(() =>
            {
                using (BusinessEntities dbContext = new BusinessEntities())
                {

                    var customer = dbContext.Customers1.First(c => c.CustomerID == Upcustomer.CustomerID);

                    customer.Fname = Upcustomer.Fname;
                    customer.Lname = Upcustomer.Lname;
                    customer.Age = Upcustomer.Age;
                    customer.Adderss = Upcustomer.Adderss;
                    customer.ContactNo = Upcustomer.ContactNo;

                    dbContext.SaveChanges();
                    // return EmptyResult;
                    // return Json(customers, JsonRequestBehavior.AllowGet);
                    //return View(customers);
                }
            });

    }

1 个答案:

答案 0 :(得分:0)

我不知道你的控制器方法在哪里声明,但我建议它应该是这样的:

$scope.getAllCustomers = function(){...}

然后在回调函数中:

}).success(function () {
    $scope.getAllCustomers();

如果这不是您的意思,请更清楚地指明问题^^