ASP MVC API IEnumerable JSON数组缺少键名

时间:2013-11-17 16:25:07

标签: c# asp.net-mvc json

我有一个ASP MVC API控制器,它将IEnumerable数组作为JSON返回 但是我没有得到数组的关键名称

[1]
0:  {
$id: "1"
id: 1
itemid: "Flame 19#Bag Acosta Produce Ctn A"
descrip: "Flame grape very suite on Carton Box"
createdate: "2013-11-17T06:26:50.047"
/...

我需要下面的库存密钥,

inventory:[1]
0:  {
$id: "1"
id: 1
itemid: "Flame 19#Bag Acosta Produce Ctn A"
descrip: "Flame grape very suite on Carton Box"
createdate: "2013-11-17T06:26:50.047"
/...

代码

public class DtoController : ApiController
{
    private ProduceServiceContext db = new ProduceServiceContext();

    public IEnumerable<InventoryDTO> MapInventories()
    {
        return from i in db.Inventories
        select new InventoryDTO() { id = i.InventoryId, createdate = i.CreateDate ?? DateTime.MinValue, descrip = i.Descriptions,
            itemid = i.ItemDetail, gtin = i.GTIN, lastcost = i.LastCost
/...

1 个答案:

答案 0 :(得分:1)

return new { inventory = from i in db.Inventories
        select new InventoryDTO() { id = i.InventoryId, createdate = i.CreateDate ?? DateTime.MinValue, descrip = i.Descriptions,
            itemid = i.ItemDetail, gtin = i.GTIN, lastcost = i.LastCost };