WebApi C#Rest Web服务无法传递对象列表

时间:2018-07-19 15:18:49

标签: c# rest asp.net-web-api

我正在构建一个简单的Rest Full Web服务,以将数据存储到数据库中。

这是我的代码:

WHEN in_tariff_length = 36 THEN round( ( ( (in_agreed / 100) * 80) * in_uplift) / 100,3) * 3

这是ActivityExecutionDTO类:

[Route("InsertActivities")]
[HttpPost]
public HttpResponseMessage InsertActivities(ActivityExecutionDTO u)
{
    try
    {
        if (u != null)
        {
            //TO DO
        }

    }
    catch (Exception e)
    {

    }
}

ActivityExecutionDetailDTO类:

namespace WebService.Models
{
    [DataContract(Name = "ActivityExecutionDTO")]
    public class ActivityExecutionDTO
    {
        [DataMember(Name = "Username", Order = 1)]
        public string UserName { get; set; }
        [DataMember(Name = "Password", Order = 2)]
        public string Password { get; set; }
        [DataMember(Name = "IdClinicalDocument", Order = 3)]
        public int IdClinicalDocument { get; set; }
        [DataMember(Name = "ActivityDetails", Order = 4)]
        public IQueryable<ActivityExecutionDetailDTO> ActivityDetails { get; set; }
    }
}

这是我通过PostMan客户端传递的JSON:

namespace WebService.Models
{
    [DataContract]
    public class ActivityExecutionDetailDTO
    {
        [DataMember(Name = "Id")]
        public int ID { get; set; }
        [DataMember(Name = "IdClinicalDocument"), IgnoreDataMember]
        public int IdClinicalDocument { get; set; }
        [DataMember(Name ="IdActivity")]
        public int IdActivity { get; set; }
        [DataMember(Name = "DateExecution"), IgnoreDataMember]
        public DateTime? DateExecution { get; set; }
        [DataMember(Name = "Duration")]
        public Int32? Duration { get; set; }
        [DataMember(Name = "CalBurned")]
        public Int32? CalBurned { get; set; }
        [DataMember(Name = "Steps")]
        public Int32? Steps { get; set; }
    }
}

因此,如果我尝试检查我的代码,则此代码:

{
    "Username" : "t.g",
    "Password" : "t",
    "IdClinicalDocument" : 63,
    "ActivityDetails" :
    [
        {
            "Id" : 1,
            "IdClinicalDocument" : 63,
            "IdActivity" : 1,
            "DateExecution" : null,
            "Duration" :  150,
            "CalBurned" : 9600,
            "Steps" :  150
        }
    ]
}

每次都是null。

活动列表为空。错误在哪里?

1 个答案:

答案 0 :(得分:0)

IQueryable<ActivityExecutionDetailDTO>更改为ICollection<ActivityExecutionDetailDTO>

IQueryable仅存储查询定义,其中不能包含任何数据,但可以存储ICollection。