Javascript 如何将参数从ActionResult传递到JsonResult?
如何将Id参数(我已经传入了Id参数)从ActionResult传递到JsonResult?因为现在我无法将Id数据传递给JsonResult参数,所以它无法命中以下JsonResult代码 我使用angularjs显示表的列表Javascript 如何将参数从ActionResult传递到JsonResult?,javascript,c#,angularjs,asp.net-mvc,Javascript,C#,Angularjs,Asp.net Mvc,如何将Id参数(我已经传入了Id参数)从ActionResult传递到JsonResult?因为现在我无法将Id数据传递给JsonResult参数,所以它无法命中以下JsonResult代码 我使用angularjs显示表的列表 [HttpGet] public ActionResult ManageCustomerStocks(Int64 Id) { return View(); } public JsonResult GetStocksByCustomerId(Int64 Id)
[HttpGet]
public ActionResult ManageCustomerStocks(Int64 Id)
{
return View();
}
public JsonResult GetStocksByCustomerId(Int64 Id)
{
List<CustomerStocksVM> model = new List<CustomerStocksVM>();
var stocks = _repositories.GetStocksByClientProfileId(Id);
var result = from stock in stocks
select new StocksVM()
{
Code = stock.Code,
Name = stock.Name
};
model = result.ToList();
return Json(new
{
customerstocks = model
},JsonRequestBehavior.AllowGet);
}
如果要从后端获取内容,请改用http.GET: javascript:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.reverse = true;
$scope.sortBy = function (propertyName) {
$scope.reverse = ($scope.propertyName === propertyName) ? !$scope.reverse : false;
$scope.propertyName = propertyName;
};
$http({
method: 'POST',
url: 'GetStocksByCustomer'
})
.then(function (response) {
console.log(response);
$scope.customerstocks = response.data.customerstocks ;
}, function (error) {
console.log(error);
});
}]);
function GetStocksByCustomerId(id) {
return $http.get("GetStocksByCustomer", { params: { "id":
id} })
.then(function (response) {
return response.data
})
.catch();
}
在angular服务中设置http调用您可以将包含
Id
的方法的viewmodel设置为long
属性,并使用返回视图(viewmodel)
,然后假设设置了@model viewmodel
,执行此操作:var Id='@model.Id'$http({method:'POST',url:'@url.Action(…)',data:$.param({Id:Id}))。然后(…);
http.POST是否与[HttpGet]兼容?
function GetStocksByCustomerId(id) {
return $http.get("GetStocksByCustomer", { params: { "id":
id} })
.then(function (response) {
return response.data
})
.catch();
}