![](/img/trans.png)
[英]Why the obj value is null in asp web API2 controller when the angular http.post has the correct value
[英]How to pass object and a value in angular $http.post and get those in asp .net web api
我想將多個對象和一個值從angular傳遞到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
如下所示:並且callan是單個值
{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)
{
---------
}
注意:為簡潔起見,避免使用整個代碼。
如果我僅在angular和webapi中傳遞一個參數(對象),就可以了。 但是,當我想傳遞兩個參數(對象和整數)時,它在控制台中顯示未找到錯誤。 如果我傳遞兩個參數(整數和整數),就可以了。 我的問題是我不能使用有角度的http.post方法將參數(對象和整數)傳遞給Web API嗎?
我可以得到一個參數(僅對象)。 但我想將2參數(對象和整數)傳遞給Web API。 我怎樣才能做到這一點?
$http.post(dataUrl + "StockProductApi/InsertStockdata?intValue="+x+"&object="+obj)
.then(function(response){
defer.resolve(response);
},function(error){
defer.reject(error);
});
此格式允許您發布單個項目。 您可以將對象作為項目的一部分發送(例如JSON),然后將單個參數作為url的一部分或將兩個參數都包括在項目本身中。
$http.post(webUrl, item).then(function (response) {
resolve(response)
}, function (error) {
reject(error);
});
除了我的評論,為了您的最終目標:
$http.post(url, angular.toJson({ ProductStockArray: productStockArray, Challan: challan }))
.then(
(response) => {
resolve(response);
},
(error) => {
reject(error);
});
有關如何在.net服務器上解析發送的JSON對象的信息,請參見這篇文章: 如何將json POST數據作為object傳遞給Web API方法 。
注意:請確保使用angular.toJson()
,而不要使用JSON.stringify()
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.