Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
List WebApi控制器,具有复杂对象子集合的POST对象_List_Asp.net Mvc 4_Post_Asp.net Web Api - Fatal编程技术网

List WebApi控制器,具有复杂对象子集合的POST对象

List WebApi控制器,具有复杂对象子集合的POST对象,list,asp.net-mvc-4,post,asp.net-web-api,List,Asp.net Mvc 4,Post,Asp.net Web Api,我必须发布此对象结构: public class Parent { public List<Child> Childs {get; set;} } public class Child { public String Name {get; set;} } 这是ParentController: public class ParentController : ApiController{ [HttpPost] [ActionName("DefaultA

我必须发布此对象结构:

public class Parent
{
    public List<Child> Childs {get; set;}
}

public class Child
{
    public String Name {get; set;}
}
这是ParentController:

public class ParentController : ApiController{

   [HttpPost]
   [ActionName("DefaultAction")]
   public HttpResponseMessage Post(Parent obj){
      // obj.Childs.Count is 2
      // but obj.Childs[0].Name is null
   }

}
问题是我收到一个包含两个child的父对象,但是child的名称为空


更新后,我找到了一个解决方法 这样做似乎有效:

公共类ParentController:ApiController{
[HttpPost]
[ActionName(“DefaultAction”)]
公共HttpResponseMessage Post(作业对象obj){
父p=obj.ToObject();
//p.Childs[0]。名称为Pippo
}
}

怪异-我也遇到了同样的问题,同样的解决方法也适用于我。我仍然相信我在做错事。
public class ParentController : ApiController{

   [HttpPost]
   [ActionName("DefaultAction")]
   public HttpResponseMessage Post(Parent obj){
      // obj.Childs.Count is 2
      // but obj.Childs[0].Name is null
   }

}
public class ParentController : ApiController{

   [HttpPost]
   [ActionName("DefaultAction")]
   public HttpResponseMessage Post(JObject obj){
      Parent p=obj.ToObject<Parent>();
      // p.Childs[0].Name is Pippo
   }

}