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

Viewed 847

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 enter image description here

enter image description here Could anybody help? What I'm doing wrong?

2 Answers

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; }
}
Related