IEnumerable对象返回JSON,但类似对象的IEnumerable不

时间:2016-07-21 18:25:30

标签: c# json entity-framework asp.net-web-api

我有两个类似的类。我试图看看我是否可以简单地使用一个。第一个是在我们的DataModel程序集中,并在我们的DbContext中使用。

public partial class type210_MotorCarrierFreightInvoice
{
    public int MotorCarrierFreightInvoiceId { get; set; } // MotorCarrierFreightInvoiceId (Primary key)
    public string InvoiceId { get; set; } // InvoiceId
    public DateTime BillDate { get; set; } // BillDate

    public string TrackingNumber { get; set; } // TrackingNumber

    public decimal NetShipmentCharge { get; set; } // NetShipmentCharge

    public long InterchangeTransactionDetailId { get; set; } // InterchangeTransactionDetailId

    public long? BillToAddressId { get; set; } // BillToAddressId
    public long? ConsigneeAddressId { get; set; } // ConsigneeAddressId
    public long? ShipperAddressId { get; set; } // ShipperAddressId

    // Foreign keys
    public virtual interchange_TransactionDetail interchange_TransactionDetail { get; set; } // fk_Type210_MotorCarrierFreightInvoice_Interchange_TransactionDetail
    public virtual typeAny_Address typeAny_Address_BillToAddressId { get; set; } 
    public virtual typeAny_Address typeAny_Address_ConsigneeAddressId { get; set; } 
    public virtual typeAny_Address typeAny_Address_ShipperAddressId { get; set; } 

    public type210_MotorCarrierFreightInvoice()
    {
        InitializePartial();
    }

    partial void InitializePartial();
}

第二个类应代表同一个对象。

public class Type210MotorCarrierFreightInvoice
{
    public int MotorCarrierFreightInvoiceId { get; set; }
    public string InvoiceId { get; set; }
    public DateTime BillDate { get; set; }

    public string TrackingNumber { get; set; }

    public decimal NetShipmentCharge { get; set; }
    public long InterchangeTransactionDetailId { get; set; }
    public Address BillToAddress { get; set; }
    public Address ConsigneeAddress { get; set; }
    public Address ShipperAddress { get; set; }
}

我正在使用WebApi服务,该服务将通过JSON公开数据。基本上我正在尝试的是:

[Route("api/Subscriber/Get210sNew/{transactionId}")]
[Route("api/Subscriber/Get210sNew/{transactionId}/{limit}")]
public IEnumerable<type210_MotorCarrierFreightInvoice> Get210sDOES_NOT_WORK(int transactionId, int limit = DefaultLimit)
{
    var key = new QueryAfterId(transactionId, limit);
    var dataService = new Type210DataService(_ediContext);
    return dataService
            .Get(key)
            .Select(x => x);
}

[Route("api/Subscriber/Get210s/{transactionId}")]
[Route("api/Subscriber/Get210s/{transactionId}/{limit}")]
public IEnumerable<type210_MotorCarrierFreightInvoice> Get210sWorks(int transactionId, int limit = DefaultLimit)
{
    var key = new QueryAfterId(transactionId, limit);
    var dataService = new Type210DataService(_ediContext);
    return dataService
            .Get(key)
            .Select(Map);
}

以下是Map方法:

private static Type210MotorCarrierFreightInvoice Map(
    type210_MotorCarrierFreightInvoice dbNotice)
{
    return new Type210MotorCarrierFreightInvoice
    {
        MotorCarrierFreightInvoiceId = dbNotice.MotorCarrierFreightInvoiceId,
        InvoiceId = dbNotice.InvoiceId,
        BillDate = dbNotice.BillDate,
        ShipDate = dbNotice.ShipDate,
        TrackingNumber = dbNotice.TrackingNumber,
        PurchaseOrderNumber = dbNotice.PurchaseOrderNumber,
        BillOfLading = dbNotice.BillOfLading,
        NetShipmentCharge = dbNotice.NetShipmentCharge,
        InterchangeTransactionDetailId = dbNotice.InterchangeTransactionDetailId,
        BillToAddress = Mapper.MapToAddress(dbNotice.typeAny_Address_BillToAddressId),
        ConsigneeAddress = Mapper.MapToAddress(dbNotice.typeAny_Address_ConsigneeAddressId),
        ShipperAddress = Mapper.MapToAddress(dbNotice.typeAny_Address_ShipperAddressId)
    };
}

Get210sWorks会将Json数据返回给浏览器。 Get210sDOES_NOT_WORK没有。两个查询都获得相同数量的记录。但Get210sDOES_NOT_WORK在浏览器窗口中没有显示任何记录。

为什么我不能将我的DataModel类的IEnumerable作为Json返回?

0 个答案:

没有答案