0
如何從ActionResult傳遞Id參數(我已經傳入Id參數)到JsonResult?因爲現在我無法將Id數據傳遞給JsonResult參數,所以它無法打到下面的JsonResult代碼。如何將參數從ActionResult傳遞給JsonResult?
我使用angularjs來顯示錶的列表。
[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);
}
的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);
});
}]);
你可以使用viewmodel作爲'HttpGet'方法,包含'Id'作爲'long'屬性,使用'return View(viewModel)',然後像這樣做,假設'@model ViewModel'被設置爲:'var id ='@Model 。ID'; $ http({method:'POST',url:'@ Url.Action(...)',data:$ .param({Id:id}))。then(...);' –
http.post與[HttpGet]兼容? – Ferus7