I have an API that receives a post, if I only receive a single contract, it receives the data normally:
[HttpPost("GravaContratos")]
public async Task<JsonResult> GravaContratos(ContratoModel contrato)
{
//faz alguma coisa...
}
Post in javascript sending only one record:
$.post("/api/Contratos/GravaContratos/", that.Contratos()[0])
.done(function (response) {
console.log(response);
});
But if I send a list or an object that contains a list, it is not receiving. I have already tried to send the list directly:
$.post("/api/Contratos/GravaContratos/", that.Contratos())
.done(function (response) {
console.log(response);
});
Or as an object:
var lista = {
Contratos: that.Contratos()
}
$.post("/api/Contratos/GravaContratos/", lista )
.done(function (response) {
console.log(response);
});
And in the API I tried to get the list:
[HttpPost("GravaContratos")]
public async Task<JsonResult> GravaContratos(<List>ContratoModel contrato)
{
//faz alguma coisa...
}
Or receive the object:
[HttpPost("GravaContratos")] // dentro da model tem uma lista de contratos
public async Task<JsonResult> GravaContratos(ContratoListaModel contrato)
{
//faz alguma coisa...
}
But by no means can I get the list of contracts, I do not know what the problem is, I've made other registrations like this and they work without problems.
Contract Model (Simple Test Model):
public class ContratoModel
{
public Guid Id { get; set; }
public string Nome { get; set; }
public string DataReajuste { get; set; }
public string DataFinal { get; set; }
public decimal Valor { get; set; }
public string NomeLocatario { get; set; }
public string NomeProprietario { get; set; }
public string NomeContrato { get; set; }
public string NomeImovel { get; set; }
}
Json uploaded from the list:
[
{
"Id":"4cd0cd37-1768-43fb-1a4b-08d3baf8b9e0",
"Nome":null,
"DataReajuste":null,
"DataFinal":null,
"Valor":316.67,
"NomeLocatario":"Cliente teste",
"NomeProprietario":"Cliente teste",
"NomeContrato":"Contrato Um",
"NomeImovel":"teste",
{
"Id":"a86c2c21-9727-453c-605f-08d3c7645118",
"Nome":null,
"DataReajuste":null,
"DataFinal":null,
"Valor":800,
"NomeLocatario":"Cliente teste",
"NomeProprietario":"Cliente teste",
"NomeContrato":"Contrato dois",
"NomeImovel":"teste"
}
]
Json sent from object:
{"Contratos":[
{
"Id":"4cd0cd37-1768-43fb-1a4b-08d3baf8b9e0",
"Nome":null,
"DataReajuste":null,
"DataFinal":null,
"Valor":316.67,
"NomeLocatario":"Cliente teste",
"NomeProprietario":"Cliente teste",
"NomeContrato":"Contrato Um",
"NomeImovel":"teste",
{
"Id":"a86c2c21-9727-453c-605f-08d3c7645118",
"Nome":null,
"DataReajuste":null,
"DataFinal":null,
"Valor":800,
"NomeLocatario":"Cliente teste",
"NomeProprietario":"Cliente teste",
"NomeContrato":"Contrato dois",
"NomeImovel":"teste"
}
]
}
I tried a post using $.ajax
, but did not receive too:
var settings = {
"async": true,
"crossDomain": true,
"url": "/api/Contratos/GravaContratos/",
"method": "POST",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache"
},
"processData": false,
"data": that.Contratos()
}
$.ajax(settings).done(function (response) {
console.log(response);
});
How can I get the list?