Hello good community I am having problems when doing a POST from Angular, I have a web service developed with ASP.NET Core 2. My post-service method is as follows:
[HttpPost]
public async Task<IActionResult> PostTipoPago(TipoPago tipoPago)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.TipoPago.Add(tipoPago);
await _context.SaveChangesAsync();
return Ok(tipoPago);
}
The TipoPago
class is the following:
public class TipoPago
{
[Key]
public int TipoPagoId { get; set; }
[MinLength(5), MaxLength(25), Required]
public string Nombre { get; set; }
[MinLength(10), MaxLength(100), Required]
public string Detalle { get; set; }
}
Now I will show you how Angular intends to send the data so that my method of service can receive them.
addTipoPago(tipoPago: TipoPago)
{
var datos = JSON.stringify(tipoPago);
let headers = new HttpHeaders().set('Content-Type', 'application/json');
return this.http.post<TipoPago>('http://localhost:52480/api/TipoPagos', datos, { headers });
}
and the model TipoPago
from Angular is as follows:
export class TipoPago
{
TipoPagoId: number;
Nombre: string;
Detalle: string;
}
and the error that I have when I execute the request is the following:
In the image you see that first I print the data that I would send to the service method, and what is happening to me is that it returns an error to me as if I was sending the data wrong, in such a way that it tells me that fields are required, as if you were receiving an empty value. I can not find what the problem could be if someone could help me would be of great help. Greetings