且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Asp.net Core 3.1 Web API请求与未绑定的对象数组

更新时间:2022-06-02 22:29:26

我认为您需要将QuestionaireAnswerRequestModel字段设置为Answers属性,例如.添加{get;放;}:

I think that you need to make QuestionaireAnswerRequestModel field Answers a property eg. add { get; set; } to it:

public class QuestionaireAnswerRequestModel
{
    public string UserName { get; set; }
    public IEnumerable<QuestionAnswerRequestModel> Answers { get; set; }
}

从MSDN:

复杂类型必须具有公共默认构造函数和公共可写属性才能绑定.发生模型绑定时,将使用公共默认构造函数实例化该类.

A complex type must have a public default constructor and public writable properties to bind. When model binding occurs, the class is instantiated using the public default constructor.

来源: https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/model-binding?view = aspnetcore-3.1