MVC Rest API - Unable to get Collection in json parameter

若如初见. 提交于 2019-12-25 08:23:22

问题


I'm trying to send this json to my controller

{  
   "Name":"Ford Focus",
   "CarProperties":[  
      {
         "Name":"Airbag"
      }
   ]
}

As you see there is a Collection named as CarProperties. I want to handle this collection in my Controller function. But getting null.

Controller

public class TestController : ApiController
{

    public CarViewModel PostCar(CarViewModel car)
    {
        //Here is the error
        // Output is : {"Name":"Ford Focus"} so, car.CarProperties has come as null
        return car;
    }

}

View Models

public class CarPropertyViewModel {
    public string Name;
}

public class CarViewModel
{
    public string Name;
    public List<CarPropertyViewModel> CarProperties { get; set; }
}

What I am doing wrong?

来源:https://stackoverflow.com/questions/40545603/mvc-rest-api-unable-to-get-collection-in-json-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!