API does not receive list post

Asked

Viewed 131 times

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 methodically method: 'POST' and data: {variavel de valor} this date to put the post value, and something else if put that.Contratos()[0] and I didn’t understand.

  • I tried to do with $.ajax, also not received, I will update the question, Contracts is a observableArray of knockout, with that.Contratos()[0] I’m sending the object from the first position.

  • 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.

  • I am debugging the API and there receives as null or as an empty list, the problem is not in the return.

2 answers

0

In the date parameter of your ajax try the following:

"data": { contratos: that.Contratos() },

And in the corresponding method:

public async Task<JsonResult> GravaContratos(List<ContratoModel> contratos)

0


The problem was that I had a model with a list of contracts in it, so:

public class ContratoListaModel{

    public IEnumerable<Contrato> Contrato { get; set; }

}

And I was getting in the API the parameter with the same name:

[HttpPost("GravaContratos")] 
public async Task<JsonResult> GravaContratos(ContratoListaModel contrato)
{
   //faz alguma coisa...

}

For some reason when it has the same names, the API does not receive, even if the difference between capital and minuscule, I changed the name of the API parameter and received normal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.