2016-10-08 11 views
0

私は、PupilsScoresのリストを持つオブジェクトを送信するサーバーにhttp putメソッドを実行しています。コレクションを含む投稿されたオブジェクトがHTTP本文のコンテンツにバインドされていません

TestIdはバインドされていますが、PupilsScoresコレクションはnullです。

私も[FromBody]を試しましたが、これはデフォルトのままにしておく必要があります。

なぜ私のPupilsScoresコレクションはnullですか?

CLIENT

 updateTestAssignment(pupilsScores, testId) { 
    return this.http.put('/api/testassignments/' + testId, { pupilsScores: pupilsScores }).map(res => res.json()); 
     } 

サーバ

public class UpdateTestAssignmentRequestDto 
{ 
    public int TestId { get; set; } 

    public IEnumerable<PupilsScoresRequestDto> PupilsScores { get; set; } 
} 

[HttpPut("~/api/testassignments/{testId:int}")] 
public async Task<IActionResult> Put(UpdateTestAssignmentRequestDto dto) 
{ 

    return NoContent(); 
} 

答えて

0

あなたもContent-Typeを指定する必要があります。デフォルト値はtext/plainです。

import { Headers, RequestOptions } from '@angular/http'; 

let headers = new Headers({ 'Content-Type': 'application/json' }); // for ASP.NET MVC 
let options = new RequestOptions({ headers: headers }); 

this.http.post(url, JSON.stringify(product), options).map(....) 
+0

RequestOptionsについて確認してください。 => http://stackoverflow.com/questions/37595980/angular-2-http-and-not-setting-content-type-application-json、とにかく、私は両方のオプションを試しても機能しません。 JSON stringifyは、content-typeがjsonの最後の2回のリリースではもう必要ありません。 – Pascal

+0

OK [FromBody]の設定とjson.stringifyを明示的に混ぜていないが、このリンクに続いてRequestOptionsはありません。http://stackoverflow.com/questions/37595980/angular-2-http-and-not-setting-content-type -application-jsonはそれをついに実現しました。私はまた、ルートからのTestIdを再びPayloadに入れて、FromBodyとバインドする必要がありました。 – Pascal

+0

RequestOptionsはここで言及されています。https://angular.io/docs/ts/latest/guide/server-communication.html – VahidN

関連する問題