简体   繁体   中英

.Net Core 3.1 WebAPI How to serialize IEnumerable<T> from POST body

I have method in controller:

[HttpPost("add")]
public async Task<IActionResult> Post([FromBody]IEnumerable<TrainingModel> model)

I have model TrainingModel :

public class TrainingModel
    {
        public int TrainingId { get; set; }
        public string Name { get; set; }
        public float Distance { get; set; }
        public int ElapsedTime { get; set; }
        public DateTime Date { get; set; }
        public string MapPolyline { get; set; }
        public int UserId { get; set; }
    }

Next query returning empty object using Postman 在此处输入图像描述

在此处输入图像描述 Could anybody help? What I'm doing wrong?

Was a problem with TrainingId property, given value for this property caused an overflow. Thanks for your help.

I ran into similar issue. For me it was because for some reason the model I was trying to deserialize into did not contain [DataContract] [DataMember] attributes. Once I added those in everything deserialized as expected:

[DataContract]
public class PersonViewModel
{
        [DataMember(Name = "address", EmitDefaultValue = true)]
        public string Address { get; set; }
}

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