API does not receive post with list

3

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?

    
asked by anonymous 19.08.2016 / 17:14

2 answers

0

The problem was that I had a model with a list of contracts inside, like this:

public class ContratoListaModel{

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

}

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

[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 the difference between uppercase and lowercase, I changed the name of the API parameter and received normal.

    
01.09.2016 / 15:13
0

In your ajax date parameter try the following:

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

And in the corresponding method:

public async Task<JsonResult> GravaContratos(List<ContratoModel> contratos)
    
22.08.2016 / 04:35