Reputation: 3
How can I pass the Id parameter(I already pass in the Id parameter) from ActionResult to JsonResult? Because now I cannot pass in the Id data to JsonResult parameter, so it cannot hit the following JsonResult code.
I using angularjs to display the List of table.
[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);
});
}]);
Upvotes: 0
Views: 559
Reputation: 727
If you want to GET something from the backend, use http.get instead:
javascript:
function GetStocksByCustomerId(id) {
return $http.get("GetStocksByCustomer", { params: { "id":
id} })
.then(function (response) {
return response.data
})
.catch();
}
Set your http calls in an angular service
Upvotes: 0
Reputation: 3510
$http({
url: 'request-url',
method: "POST",
data: { 'id' : urId }
})
.then(function(response) {
// success
},
function(response) { // optional
// failed
});
Upvotes: 1