2016-10-08 1 views
0

PupilsScores 목록이있는 개체를 보내는 서버에 http put 메서드를 사용하고 있습니다.컬렉션이있는 게시 된 개체가 http 본문 내용에 바인딩되지 않습니다.

TestId는 바인딩되어 있지만 PupilsScores 컬렉션은 null입니다.

나는 또한 [FromBody]을 시도했으나 기본적으로 게시물/put을한다고 가정해야합니다.

내 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

[FromBody] 설정을 명시 적으로 조합하고 json.stringify를 사용하지 않고이 링크 다음에 오는 RequestOptions이 없으면 http : //stackoverflow.com/questions/37595980/angular-2-http-and-not-setting-content-type을 입력하십시오. -application-json이 결국 그것을 만들었습니다. 또한 FromInd와 바인딩되도록 Payload에 다시 TestId를 경로에서 가져와야했습니다. – Pascal

+0

'RequestOptions'는 여기에 언급되어 있습니다. https://angular.io/docs/ts/latest/guide/server-communication.html – VahidN

관련 문제