将集合发布到OData Controller不会将嵌套的集合属性返回给客户端 [英] Posting Collection to OData Controller not returning nested collection property to client

查看:94
本文介绍了将集合发布到OData Controller不会将嵌套的集合属性返回给客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下Model类:

I have the following Model class defined:

public class LinePostModel
    {
        [Key]
        public int OrderId { get; set; }
        public IList<OrderLine> OrderLines { get; set; }
    }

以及我的OData Controller上的此方法:

and this method on my OData Controller:

public async Task<IHttpActionResult> Post(LinePostModel model)
{
  //validae model and its OrderLines collection and save to repo.

  return Created(model);
}

服务器正确接收模型对象,并处理每个查找.我的问题是,返回给客户端的RESPONSE仅包含原始的OrderId属性,而没有包含集合属性OrderLines.

The server receives the model object correctly, and processes every find. My problem is that the RESPONSE that is returned to the client only contains the primitive OrderId property, and does NOT contain the collection property OrderLines.

如何解决此问题,以便将整个复杂对象返回给客户端?

How can I fix this so that the entire complex object is returned to the client?

添加了示例json:

客户在这里发布的内容:

Here is what the client posts:

{
      orderId:4,
      orderLines: [
        {
            lineType: "S",
            orderQty: 10,
            priceCode: "D",
            stockCode: "62.C6W026A.050",
            warehouse: "GS",
            orderId:4
        },
        {
            lineType: "S",
            orderQty: 20,
            priceCode: "D",
            stockCode: "62.C6W026A.060",
            warehouse: "GS",
            orderId:4
        }
      ]
}

这是客户端从服务器收到的回馈:

and here is what the client receives back from the server:

{
  "@odata.context": "http://localhost:4095/$metadata#OrderLines/$entity",
  "orderId": 4
}

我尝试将服务器更改为返回Ok(model)而不是Created(model),但这没什么区别.

I have tried changing the server to return Ok(model) instead of Created(model), but it makes no difference.

控制器方法中return Created(model)行上的断点表明,在那一点上,模型对象肯定包含OrderLines的集合.

A breakpoint on the return Created(model) line in the controller method shows that at that point the model object definitely contains the collection of OrderLines.

推荐答案

我遇到了同样的问题.对我而言,OrderLine被定义为实体类型.同一类型不能同时是实体类型和复杂类型,因为复杂类型没有'Id'属性.确保未将OrderLine设置为EntityType.如果这样不起作用,请尝试将其明确设置为"ComplexType".

I had the same issue. For me OrderLine was defined as an entity type. Same type cannot be both entity and complex type because complex type does not have an 'Id' property. Make sure OrderLine is not set as EntityType. If that does not work, try setting it as 'ComplexType' explicitly.

这篇关于将集合发布到OData Controller不会将嵌套的集合属性返回给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆