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:
[DataContract]
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"
}
Full code from Postman :
POST /api/employees HTTP/1.1
Host: localhost:49463
Content-Type: application/json
cache-control: no-cache
Postman-Token: 5ac38b20-425a-4ac7-995a-323fbd0bb9a5
{
"FirstName" : "John",
"LastName" : "Malon",
}------WebKitFormBoundary7MA4YWxkTrZu0gW--
EDIT : It seems that the emp object that I receive in the Post method is null
I saw this post : Post parameter is always null
But english is not my primary language and I cant figure out what is the solution with the =
I understand that I need to put an = in the JSON that I send, is that right ?
Thank you in advance for your help!