如何将参数从ActionResult传递给JsonResult?

时间:2017-06-06 09:01:18

标签: 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)
{
   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);
    });
}]);

2 个答案:

答案 0 :(得分:1)

 $http({
        url: 'request-url',
        method: "POST",
        data: { 'id' : urId }
    })
    .then(function(response) {
            // success
    }, 
    function(response) { // optional
            // failed
    });

答案 1 :(得分:0)

如果您想从后端获取内容,请改用http.get:

的javascript:

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

在角度服务中设置您的http呼叫