简体   繁体   中英

Web API Restful service ReadAsAsync<type> not returning lists

I am trying to return lists of objects inside of a class, however when I try to read the result into the container class it nulls out the lists. I am pretty new to Web API so I don't know if this is by design or if I am doing something wrong. If I read the response content into a string the lists are there. The first image shows that the lists are intact and the second shows that they are no longer available. Any direction would be appreciated! 建立回应readasasync <t>时为空值

 using (HttpClient client = new HttpClient())
            {
                string url = Url(String.Format("{0}", id));
                SetupClient(client, url);

                HttpResponseMessage response = client.GetAsync(url).Result;

                if (response.IsSuccessStatusCode)
                {


                    shipment = response.Content.ReadAsAsync<ShipmentViewModel>().Result;

                }

Edit for adding ShipmentViewModel source

public class ShipmentViewModel
{
    public Guid Id { get; set; }

    public String ShipmentName { get; set; }

    public String Description { get; set; }

    public String Comments { get; set; }

    /// <summary>
    /// Used for UI display purposes only
    /// </summary>

    public int NumberOrdersScanned { get; set; }
    public int NumberOrdersTotal { get; set; }

    public int NumberParcelsScanned { get; set; }
    public int NumberParcelsTotal { get; set; }

    public List<ShipmentEntity> ShipmentEntities { get; set; }

    public ShipmentEntity ShipmentEntity { get; set; }
    public Guid ValidationTypeId { get; set; }
    //        public string ValidationType { get; set; }
    public ValidationType ValidationType { get; set; }
    public List<ValidationType> ValidationTypes { get; set; }
    /// <summary>
    /// Used for UI display purposes only
    /// </summary>
    public List<ShipmentStatus> ShipmentStatuses { get; set; }

    public ShipmentStatus ShipmentStatus { get; set; }

    /// <summary>
    /// Used for UI display purposes only
    /// </summary>
    public List<ShipmentDestinationCountry> DestinationCountries { get; set; }

    public ShipmentDestinationCountry DestinationCountry { get; set; }

    private List<ShipmentTrackingNumber> _TrackingNumbers;
    public List<ShipmentTrackingNumber> TrackingNumbers
    {
        get
        {
            return _TrackingNumbers ?? (_TrackingNumbers = new List<ShipmentTrackingNumber>());
        }
        set
        {
            _TrackingNumbers = value;
        }
    }

    public bool RequiresValidation { get; set; }
    private List<ShipmentValidationViewModel> _Validations { get; set; }

    public List<ShipmentValidationViewModel> Validations
    {
        get
        {
            return _Validations ?? (_Validations = new List<ShipmentValidationViewModel>());
        }
        set
        {
            _Validations = value;
        }
    }

    public Guid EdiShipmentInfoId { get; set; }
    public EdiShipmentInfoViewModel EdiShipmentInfo { get; set; }

    public ShipmentEvents Events { get; set; }
}

Here is the EdiShipmentInfoViewModel

 public class EdiShipmentInfoViewModel
{
    //        private string _packagingCode;
    public Guid Id { get; set; }
    public Guid ShipmentId { get; set; }
    public Guid WarehouseId { get; set; }
    public virtual InternalLookupModel Warehouse { get; set; }
    public DateTime? ShippingDate { get; set; }
    public DateTime? DeliveryDate { get; set; }
    public string EquipmentNumber { get; set; }

    public string ShippingWeight { get; set; }

    public string PackagingCode { get; set; }

    public string CarrierCommonName { get; set; }
    public Guid ScacCodeId { get; set; }
    public virtual InternalLookupModel ScacCode { get; set; }
    public Guid PackagingFormId { get; set; }
    public virtual InternalLookupModel PackagingForm { get; set; }
    public Guid PackagingMaterialId { get; set; }
    public virtual InternalLookupModel PackagingMaterial { get; set; }

    public string MasterBillLading { get; set; }
    public string BillLading { get; set; }
    public string LadingDescription { get; set; }
    public string FobShippingPoint { get; set; }
    public string FobDestination { get; set; }
    public Guid TransPayMethodId { get; set; }
    public virtual InternalLookupModel TransPayMethod { get; set; }
    public Guid TransTypeId { get; set; }
    public virtual InternalLookupModel TransType { get; set; }
    public Guid ServiceLevelCodeId { get; set; }
    public virtual InternalLookupModel ServiceLevelCode { get; set; }
    public Guid UnitOfMeasureId { get; set; }
    public virtual InternalLookupModel UnitOfMeasure { get; set; }


    private List<InternalLookupModel> _WarehouseList;
    public List<InternalLookupModel> WarehouseList
    {
        get { return _WarehouseList ?? new List<InternalLookupModel>(); }
        set { this._WarehouseList = value; }
    }

    private List<InternalLookupModel> _ScacCodeList;

    public List<InternalLookupModel> ScacCodeList
    {
        get { return _ScacCodeList ?? new List<InternalLookupModel>(); }
        set { this._ScacCodeList = value; }
    }
    private List<InternalLookupModel> _PackagingFormList;
    public List<InternalLookupModel> PackagingFormList
    {
        get { return _PackagingFormList ?? new List<InternalLookupModel>(); }
        set { this._PackagingFormList = value; }
    }

    private List<InternalLookupModel> _PackagingMaterialList;
    public List<InternalLookupModel> PackagingMaterialList
    {
        get { return _PackagingMaterialList ?? new List<InternalLookupModel>(); }
        set { this._PackagingMaterialList = value; }
    }

    private List<InternalLookupModel> _TransPayMethodList;
    public List<InternalLookupModel> TransPayMethodList
    {
        get { return _TransPayMethodList ?? new List<InternalLookupModel>(); }
        set { this._TransPayMethodList = value; }
    }

    private List<InternalLookupModel> _TransTypeList;
    public List<InternalLookupModel> TransTypeList
    {
        get { return _TransTypeList ?? new List<InternalLookupModel>(); }
        set { this._TransTypeList = value; }
    }

    private List<InternalLookupModel> _ServiceLevelCodeList;

    public List<InternalLookupModel> ServiceLevelCodeList
    {
        get { return _ServiceLevelCodeList ?? new List<InternalLookupModel>(); }
        set { this._ServiceLevelCodeList = value; }
    }

    private List<InternalLookupModel> _UnitOfMeasureList;

    public List<InternalLookupModel> UnitOfMeasureList
    {
        get { return _UnitOfMeasureList ?? new List<InternalLookupModel>(); }
        set { this._UnitOfMeasureList = value; }
    }
}

I think @BrianBall is correct. Rewrite your class so that your list properties are simple getters/setters and initialize the lists in your constructor.

public class ShipmentViewModel
{
  public ShipmentViewModel()
  {
    TrackingNumbers = new List<ShipmentTrackingNumber>();
  }

  public List<ShipmentTrackingNumber> TrackingNumbers { get; set; }
  // and so on
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM