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(); } 

1 Answer 1

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(....) 
Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure about RequestOptions? => stackoverflow.com/questions/37595980/… , 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.
Ok the mix of setting [FromBody] explicitly AND no json.stringify AND no RequestOptions but following this link:stackoverflow.com/questions/37595980/… 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.
I tested it now with RequestOptions too and it worked too. Thus please change your answer including my findings server/client side :-) thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.