3
I have an API that receives a post, if I receive only a single contract, it receives the data normally:
[HttpPost("GravaContratos")]
public async Task<JsonResult> GravaContratos(ContratoModel contrato)
{
//faz alguma coisa...
}
Javascript post 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’ve tried to send the list direct:
$.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 no way I can get the list of contracts, I don’t know what the problem is, I’ve done other registrations like this and they work without problems.
Contract Model (Simple Model for Testing):
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 sent 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 the 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 either:
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 receive the list?
$.post
it’s different to use date, use$.ajax
methodicallymethod: 'POST'
anddata: {variavel de valor}
this date to put the post value, and something else if putthat.Contratos()[0]
and I didn’t understand.– KingRider
I tried to do with
$.ajax
, also not received, I will update the question, Contracts is aobservableArray
ofknockout
, withthat.Contratos()[0]
I’m sending the object from the first position.– Aesir
If this returned the value? if it is not returned and it was that problem, it will follow your ajax before parentese and add
return {variavel resultado}
and try one more time.– KingRider
I am debugging the API and there receives as
null
or as an empty list, the problem is not in the return.– Aesir