2017-06-06 6 views
0

ActionResultからJsonResultにIdパラメータを渡すにはどうしたらいいですか?今は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

'return view(viewModel)'で 'long'プロパティとして' Id'を含む 'HttpGet'メソッドに対してviewmodelを使うことができます。' @model ViewModel'がセットされていると仮定して、このようにします: 'var id = '@Model .Id '; $({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コール

関連する問題