【发布时间】: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