ASP.NET Core中的属性绑定不绑定属性值以查看模型

时间:2019-12-24 06:39:45

标签: angular asp.net-core

我试图弄清楚为什么我无法将JSON对象发送到后端api。鉴于以下代码,我可以访问我的api端点,但是Name: Test从未到达控制器:

Angular http请求:

addSport(newSportType: SportType): Observable<SportType> {
const headers = {
            headers: new HttpHeaders({
                'Content-Type': 'application/json'
            })
        };
        return this.http.post<SportType>('/sport-type', JSON.stringify({ Name: 'Test' }), headers);
    }

enter image description here

我的TestViewModel如下:

public class TestViewModel

    {
        public string Name { get; set; }
    }

此设置中是否缺少基本的内容?

1 个答案:

答案 0 :(得分:2)

使用[FromBody]

public ... ([FromBody]TestViewModel newSportType)
{

}