【问题标题】:Child Entity/class always return null in RESTFul WCF子实体/类在 RESTFul WCF 中始终返回 null
【发布时间】:2016-12-01 08:40:32
【问题描述】:

我的班级结构是这样的

namespace Eauction
{
[DataContract(Namespace = "")]
public class SaleListJS
{
    public SaleListJS()
    {
        this.SaleDataJS = new HashSet<SaleDataJS>();
    }
    [DataMember]
    public string AuctionId { get; set; }
    [DataMember]
    public int DepotId { get; set; }
    [DataMember]
    public string Depot_Name { get; set; }
    [DataMember]
    public string Sale_Date { get; set; }

    public virtual ICollection<SaleDataJS> SaleDataJS { get; set; }

}

[DataContract(Namespace = "")]
public class SaleDataJS
{
    [DataMember]
    public string SNo { get; set; }
    [DataMember]
    public string Speices_Name { get; set; }
    [DataMember]
    public string LotNo_Year { get; set; }
    [DataMember]
    public string Stack_No { get; set; }

    [DataMember]
    public string Grade { get; set; }
    [DataMember]
    public string Length { get; set; }
    [DataMember]
    public string Mid_Girth { get; set; }
    [DataMember]
    public string No_Of_Piece { get; set; }

    [DataMember]
    public string Volume { get; set; }
    [DataMember]
    public string Floor_Price { get; set; }
    [DataMember]
    public string AuctionId { get; set; }

    public virtual SaleListJS SaleListJS { get; set; }
}

}

和这样的服务

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "uploadSalelist")]
    string UploadSalelist(SaleListJS sl);
}

public class Service1 : IService1
{
    public string UploadSalelist(SaleListJS sl)
    {
        string returnBool = "false";
        if (sl != null)
        {
            try
            {
                string depotId = sl.DepotId.ToString();
                string depotName = sl.Depot_Name;
                string auctionDate = sl.Sale_Date.ToString();


                if (sl.SaleDataJS != null)
                {
                    foreach (SaleDataJS sld in sl.SaleDataJS)
                    {

                    }
                }                  

                returnBool = "true";
            }
            catch (Exception ex)
            {
                returnBool = ex.Message;
            }
        }
        return returnBool;
    }
}

当我用邮递员调用它时,它总是返回 'SaleDataJS' null 但 'SaleListJS' 不为 null

我的 Json 是

{
   "sl":{
      "DepotId":"12",
      "Depot_Name":"Lucknow",
      "Sale_Date":"11\/30\/2016",
      "SaleDataJS":[
         {
            "SNo":"1",
            "Speices_Name":"Neem",
            "LotNo_Year":"370/NC/14-15",
            "Stack_No":"48/14-15",
            "Grade":"II",
            "Length":"0.95",
            "Mid_Girth":"121 - 150",
            "No_Of_Piece":"15",
            "Volume":"0.0928",
            "Floor_Price":"6715"
         },
         {
            "SNo":"1",
            "Speices_Name":"Neem",
            "LotNo_Year":"370/NC/14-15",
            "Stack_No":"48/14-15",
            "Grade":"II",
            "Length":"1.95",
            "Mid_Girth":"121 - 150",
            "No_Of_Piece":"5",
            "Volume":"0.2202",
            "Floor_Price":"6715"
         }
      ]
   }
}

请帮帮我,我现在正处于危机之中。

【问题讨论】:

    标签: c# json wcf


    【解决方案1】:

    改变

    public virtual ICollection<SaleDataJS> SaleDataJS { get; set; }
    

    [DataMember]
    public virtual ICollection<SaleDataJS> SaleDataJS { get; set; }
    

    为我工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2016-05-31
      • 2015-08-15
      • 2012-03-18
      • 2016-11-04
      • 2016-07-28
      • 2010-11-08
      相关资源
      最近更新 更多