【问题标题】:Async Controller Action Method invoke by using AngularJS使用 AngularJS 调用异步控制器操作方法
【发布时间】:2015-08-20 09:21:32
【问题描述】:

我是 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);
                }
            });

    }

【问题讨论】:

  • 可以贴出你要调用的方法,目的是什么?更清楚。
  • 如果您谈论的是 ASP.NET MVC 异步操作方法,那么它在服务器端是异步的这一事实与 HTTP 客户端如何调用它无关。
  • 我编辑了我的问题,请看一下
  • 你检查过MVC网站吗?更具体地说是他们的 WebAPI 部分?
  • 有一个关于AngularJS + ASP.NET MVC的教程here

标签: angularjs asynchronous


【解决方案1】:

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

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

然后在回调函数中:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
  • 2015-02-06
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多