如何将参数从ActionResult传递到JSONRESULT



如何将ID参数(我已经通过ID参数传递(从ActionResult到JSONRESULT?因为现在我无法将ID数据传递给JSONRESULT参数,因此它无法达到以下JSONRESULT代码。

i使用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);
    });
}]);
 $http({
        url: 'request-url',
        method: "POST",
        data: { 'id' : urId }
    })
    .then(function(response) {
            // success
    }, 
    function(response) { // optional
            // failed
    });

如果要从后端获得一些东西,请改用http.get:

javaScript:

 function  GetStocksByCustomerId(id) {
    return $http.get("GetStocksByCustomer", { params: { "id": 
   id} })
   .then(function (response) {
       return response.data
    }) 
    .catch();
}

在Angular服务中设置您的HTTP调用

最新更新