0

I am doing a http put method to the server sending an object with a list of PupilsScores.

The TestId is bound, but the PupilsScores collection is null.

I also tried [FromBody] but that should be the default I assume doing a post/put.

So why is my PupilsScores collection null?

CLIENT

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

SERVER

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();
}
Pascal
  • 12,265
  • 25
  • 103
  • 195

1 Answers1

0

You need to specify the Content-Type too. Its default value is 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(....)
VahidN
  • 18,457
  • 8
  • 73
  • 117
  • Are you sure about RequestOptions? => http://stackoverflow.com/questions/37595980/angular-2-http-and-not-setting-content-type-application-json , anyway I tried both options and it does not work. JSON stringify is not needed anymore for the last angular 2 releases when content-type is json. – Pascal Oct 08 '16 at 22:40
  • Ok the mix of setting [FromBody] explicitly AND no json.stringify AND no RequestOptions but following this link:http://stackoverflow.com/questions/37595980/angular-2-http-and-not-setting-content-type-application-json made it finally WORK. I also had to put the TestId from the route also again into the payload to get bound with the FromBody. – Pascal Oct 08 '16 at 22:48
  • `RequestOptions` is mentioned here https://angular.io/docs/ts/latest/guide/server-communication.html – VahidN Oct 09 '16 at 05:21
  • I tested it now with RequestOptions too and it worked too. Thus please change your answer including my findings server/client side :-) thanks! – Pascal Oct 09 '16 at 10:17