2017-07-31 5 views
0

角度からasp .net web APIに複数のオブジェクトと値を渡したいと思います。

私のjsは:下記のようにここで

var productStockArray =$scope.ProductStockArray; 
     var challan = 20; 
    $http.post(dataUrl + "StockProductApi/InsertStockdata", { params: { ProductStockArray: productStockArray, Challan: challan } }) 
       .then(function (data) { 

productStockArrayがある:とカランは

{ProductCode: "Pr- 001", ProductName: "T-Shirt", DealerPrice: 5, profitPercentage: 5, freePc: 5} 
{ProductCode: "abc6", ProductName: "Luxs", originalPc: 455, freePc: 5, profitPercentage: 5} 

マイWEBAPI単一の値である:

[HttpPost] 
    [Route("api/StockProductApi/InsertStockdata/")] 
    public HttpResponseMessage InsertStockdata(IEnumerable<StockProduct> ProductStockArray, int Challan) 
     { 
--------- 
} 

NB:全体のコードがあります簡潔にするために避けた。

角度とwebapiで1つのパラメータ(オブジェクト)のみを渡すと問題ありません。しかし、私は2つのパラメータ(オブジェクトとint)を渡したい場合、コンソールにエラーが見つかりません。私は2つのパラメータ(intとint)を渡す場合、それは大丈夫です。 私の質問は、角度http.postメソッドを使ってWeb APIにパラメータ(オブジェクトとint)を渡すことができないのですか?

私は1つのパラメータ(オブジェクトのみ)を取得できます。しかし、私は2つのパラメータ(オブジェクトとint)をWeb APIに渡したいと思います。 どうすればいいですか?

+0

JSONを試しましたか? 'angular.toJson()' –

+0

あなたはangularjsとweb apiの例を挙げられますか? @Dunn – Fawel

+0

サーバー側からリクエストを処理する方法については、この記事を参照してください。https://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-オブジェクトとして。角度側については、単に '$ http.post(url、angular.toJson({ProductStockArray:productStockArray、Challan:challan})))' –

答えて

0
$http.post(dataUrl + "StockProductApi/InsertStockdata?intValue="+x+"&object="+obj) 
     .then(function(response){ 
      defer.resolve(response); 
     },function(error){ 
      defer.reject(error); 
     }); 
+0

を使用します。これは、 'var productStockArray = $ scope.ProductStockArray; var challan = 20; $ scope.saveLoadingGif = true; [HttpPost] [Route( "api/StockProductApi/InsertStockdata /")] パブリックHttpResponseMessage InsertStockData(InsertIdentityData) int Challan、object ProductStockArray) {'しかしオブジェクトProductStockArray = null – Fawel

0

このフォーマットでは、1つのアイテムを投稿することができます。オブジェクトをアイテムの一部(JSONなど)として送信し、単一パラメータをURLの一部として含めるか、アイテム自体に両方のパラメータを含めることができます。あなたの角度の終わりのための私のコメントにさらに

$http.post(webUrl, item).then(function (response) { 
     resolve(response) 
    }, function (error) { 
     reject(error); 
    }); 
+0

だから、どうにか私は1つのパラメータを渡す必要があると言っていますか?しかし、私は2つのint変数を渡す場合、それはAPIで見つかりました。なぜオブジェクトができないのですか? – Fawel

+0

いくつかの方法がありますが、IEnumerableが示唆するようにアイテムの配列を送信すると、URLパラメータとして問題になる可能性があります。 webUrlのパラメータとして個々の値を送信し、StockProductsの配列をアイテムの一部として送信します。 – Graham

+0

サンプルアイテムJSONオブジェクトとして、Web APIが私がその側で作成したカスタムオブジェクトタイプと見なすものを送信します。 { \t "ManualId": "ABC1234"、 \t "ChapterSections":[{ "ChapterSectionId": "123"}、 \t \t { "ChapterSectionId": "456"}、 \t \t { "ChapterSectionId" : "789"}、 \t \t { "ChapterSectionId": "10"}、\t \t { "ChapterSectionId": "11"}] } – Graham

0

、:How to pass json POST data to Web API method as objectを:

$http.post(url, angular.toJson({ ProductStockArray: productStockArray, Challan: challan })) 
.then(
(response) => { 
    resolve(response); 
}, 
(error) => { 
    reject(error); 
}); 

そして、この記事を参照して、.NETサーバー上で送信されたJSONオブジェクトを解析する方法については

注:、angular.toJson()を使用するようにしてくださいませんJSON.stringify()

グッドラック!

+0

'angular.min.js:96 POSTはhttp:// localhostを: 15822/api/StockProductApi/InsertStockdata 404(見つからない) ' – Fawel

+0

api' [HttpPost] [Route( "api/StockProductApi/InsertStockdata /")] public HttpResponseメッセージInsertStockdata(int Challan、オブジェクトProductStockArray){' – Fawel

+0

APIパラメータは何ですか? – Fawel

関連する問題