2017-06-06 114 views
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); 
    }); 
}]); 
+0

你可以使用viewmodel作爲'HttpGet'方法,包含'Id'作爲'long'屬性,使用'return View(viewModel)',然後像這樣做,假設'@model ViewModel'被設置爲:'var id ='@Model 。ID'; $ http({method:'POST',url:'@ Url.Action(...)',data:$ .param({Id:id}))。then(...);' –

+0

http.post與[HttpGet]兼容? – Ferus7

回答

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

如果你想從後端的東西,使用http.get代替:

的javascript:

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

設置你的http調用角度服務