发布嵌套模型

时间:2017-03-31 11:45:05

标签: c# asp.net asp.net-mvc django asp.net-web-api

目前我的api帖子的工作原理如下:

POST:localhost:55974 / api / trades /

{
  "id": 1,
  "baseProduct": {
    "id": 1,
    "productID": "sample string 2",
    "tags": [
      "sample string 1",
      "sample string 2"
    ]
  },
  "productDescription": "sample string 2",
  "images": [
    "QEA=",
    "QEA="
  ],
  "price": 3,
  "estimatedShippingDate": "2017-03-31T20:41:03.2760924+09:00"
}

我想要的是将现有数据与其ID相关联;像这样的东西:

{
  "id": 1,
  "baseProduct": 1,
  "productDescription": "sample string 2",
  "images": [
    "QEA=",
    "QEA="
  ],
  "price": 3,
  "estimatedShippingDate": "2017-03-31T20:41:03.2760924+09:00"
}

请注意,baseProduct已更改为其ID,但是当我尝试执行此操作时,出现此错误:

{
  "Message": "The request is invalid.",
  "ModelState": {
    "tradeModel.baseProduct": [
      "Error converting value 3 to type 'EodiRoad.Models.BaseProductModel'. Path 'baseProduct', line 2, position 19."
    ]
  }
}

这在django中运行良好,所以我猜它也会在ASP.NET上运行。  任何可能的解决方案?

===编辑==== 我的数据模型看起来像这样。

Trademodel.cs

public class TradeModel
    {
        public int id { get; set; }


        [Required]
        [ForeignKey("baseProduct")]
        public int baseProductId { get; set; }

        public virtual BaseProductModel baseProduct { get; set; }

        // public BaseProductModel baseProduct { get; set; }

        public string productDescription { get; set; }
        public List<byte[]> images { get; set; }
        public int price { get; set; }
        // Date infos
        public DateTime estimatedShippingDate { get; set; }

    }

BaseProductModel.cs

public class BaseProductModel
{

    public int id { get; set; }
    public string productID { get; set; }
    // Not in use
    public List<string> tags { get; set;}
}

0 个答案:

没有答案