带有集合的已发布对象不会绑定到http正文内容

时间:2016-10-08 22:15:03

标签: c# angular asp.net-core

我正在向服务器发送一个http put方法,发送一个带有PupilsScores列表的对象。

TestId已绑定,但PupilsScores集合为空。

我也试过了[FromBody],但这应该是我假设做一个帖子/放置的默认值。

那么为什么我的PupilsScores集合为空?

客户端

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

1 个答案:

答案 0 :(得分: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(....)
相关问题