Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api ASP.NET Web API深度模型绑定_Asp.net Web Api_Controller_Model Binding_Formatter - Fatal编程技术网

Asp.net web api ASP.NET Web API深度模型绑定

Asp.net web api ASP.NET Web API深度模型绑定,asp.net-web-api,controller,model-binding,formatter,Asp.net Web Api,Controller,Model Binding,Formatter,我注意到(甚至在WebAPI2.1中)深层参数类型仅在第一层上填充(由模型绑定器处理)。即: public class Person { public string Name { get; set; } public PersonDetails Details { get; set; } } public class PersonDetails { public string Address { get; set; } public int Age { get;

我注意到(甚至在WebAPI2.1中)深层参数类型仅在第一层上填充(由模型绑定器处理)。即:

public class Person
{
    public string Name { get; set; }
    public PersonDetails Details { get; set; }
}

public class PersonDetails
{
    public string Address { get; set; }
    public int Age { get; set; }
}

// ...

public class PersonController : ApiController
{

    [HttpPost]
    public void ProcessPerson(Person person)
    {
        // person.Name is filled in correctly
        // person.Details.Address and person.Details.Age are not filled in correctly. That is, they have default values (null and 0)
    }
}
对于这个问题有没有一个简单的解决方案,除了像这样平铺Person类之外

public class PersonData
{
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }   
}

稍后编辑1:

  • 如果我将Person类展平,我将正确获取所有数据
  • 请求是通过POST(而不是GET)发出的,因为我需要确保没有缓存,并且由于操作改变了状态,所以使用GET在语义上是不正确的

  • 您的Person对象在发布之前在客户端上是什么样子的?另外,如果您将客户端窗体绑定到PersonController中返回Person类的GET调用,它应该可以正常工作。谢谢