mercredi 27 février 2019

Modele Binding error when POST method called Web API

I come to post here after reading a lot of topics about the same mistake I have now.

I have a WEB API that works in GET but not in POST. When I send him JSON with POSTMAN, I get an error message in the ModelState which is ModelError ModelBinding

This is my model class:

public class Employee
{
        [DataMember]
        public int ID { get; set; }
        [DataMember(IsRequired = true)]
        public string FirstName { get; set; }
        [DataMember(IsRequired = true)]
        public string LastName { get; set; }
}

Here is the Post method of the controller of my API :

public IHttpActionResult Post([FromBody]Employee emp)
{
            if (!ModelState.IsValid)
                return BadRequest("Invalid data");

            ...
            return Ok();
}

In this controller, I pass in my if and it returns "invalid data" to me

I tried everything, I removed the[DataContract] in my model, but nothing works.

Here is an example of the JSON I am sending:

{
    "FirstName" : "John",
    "LastName" : "Malon"
}

Thank you in advance for your help!




Aucun commentaire:

Enregistrer un commentaire