2017-04-04 1 views
2

MVCコアコントローラに単純なangularjsページからオブジェクトをポストする際に問題があります。MVCのコアアクションがangularjsからバインドされていませんPOST

MVCアクションでのオブジェクトは、オブジェクト自体がnullではないにもかかわらず、バインドされていません。これは通常の問題です。

私が間違っていることを誰も見ることができますか?

これは私の角度サービスコードです:

によって呼び出され
this.getQuote = function (priceRequest) { 
    return $http.post('/quote/getcost', { priceRequest }); 
}; 

this.quoteDataがある
quoteService.getQuote(this.quoteData).then(function (cost) { 
    $scope.quoteData.quoteCost = cost.data; 
}); 

$scope.quoteData = { 
       detailLevel: '0', 
       fileLengthHours: 0, 
       fileLengthMinutes: 1, 
       excessiveSpeakersCount: 1, 
       industry: null, 
       deliveryTime: '1', 
       additionalInformation: '', 
       quoteCost: null 
      }; 

これは、ペイロード enter image description here

です

これはPOSTです: enter image description here

最後に私のC#MVCコアアクション:

に投稿されたオブジェクトがnullではありませんが、値のどれもがバインドされていない
[HttpPost] 
public JsonResult GetCost([FromBody]PriceRequest priceRequest) 
{ 
    var price = _priceCalculator.GetPrice(priceRequest); 

    return new JsonResult(price); 
} 

enter image description here

これはPriceRequestオブジェクトです:

public class PriceRequest 
{ 
    public JobDetailLevel DetailLevel { get; set; } 

    public int FileLengthHours { get; set; } 

    public int FileLengthMinutes { get; set; } 

    public int? ExcessiveSpeakersCount { get; set; } 

    public JobIndustry Industry { get; set; } 

    public JobDeliveryTime DeliveryTime { get; set; } 

    public string AdditionalInformation { get; set; }   
} 

誰かが私が間違っていることを見ることができますか?

+0

.Net PriceRequestオブジェクトはどのように定義されていますか? – brad

+0

Bradに質問に追加されました。文字列またはint以外のものは列挙型です – Simon

+0

"POST"のスクリーンショットには本文がありません。身体はどのように見えますか? – Liam

答えて

1

[OK]をので、この記事の礼儀: Asp.net core MVC post parameter always null

私はstartup.csにこれを追加する必要:しようとした人たちに

.AddJsonOptions(jsonOptions => 
{ 
    jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; 
}); 

感謝助けて。

0

角度側のpriceRequest変数の周りから{}を削除してみます。

同様:

return $http.post('/quote/getcost', priceRequest); 
+0

サーバー側のオブジェクトがnullになる – Simon

+0

「nullになる」ことはできません。それはにISNので、あなたは別のオブジェクトであなたのJSONオブジェクトをラップしている} {でpriceRequestを包むことで this.getQuote =機能(priceRequest){ } :あなたはこの関数にパラメータとしてJSONオブジェクトを渡していますサーバ側で身体を縛るt – brad

+0

申し訳ありませんサーバー側に編集! – Simon

関連する問題