Null ajax values for controller

Asked

Viewed 125 times

2

I’m getting values null on the subject by parameter no controller of asp.net core via ajax

Follows the javascript with the ajax

 var ClienteFornecedor = {
        Id: "0",
        Tipo: $('#selTipoAdicionar').val(),
        NomeRazaoSocial: $('#txtNomeRazaoSocialAdicionar').val(),
        NomeFantasia: $('#txtNomeFantasiaAdicionar').val(),
        CpfCnpj: $('#txtCpfCnpjAdicionar').val(),
        Endereco: $('#txtEnderecoAdicionar').val(),
        Bairro: $('#txtBairroAdicionar').val(),
        Cidade: $('#txtCidadeAdicionar').val(),
        Estado: $('#txtEstadoAdicionar').val(),
        Cep: $('#txtCEPAdicionar').val(),
        Telefone: $('#txtTelefoneAdicionar').val(),
        Celular: $('#txtCelularAdicionar').val(),
        Ie: $('#txtInscEstadualAdicionar').val(),
        Observacao: $('#txtObservacaoAdicionar').val()
    };

   var dto = JSON.stringify(ClienteFornecedor);

    $.ajax({
        url: 'ClienteFornecedor/Adicionar',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data:  dto,
        success: function (result) {

            }

        }
    });

I checked in debug and saw that you are converting correctness into json, but the problem is that in controller of the application, the object is arrived with the properties null

Just follow my controller:

    public JsonResult Adicionar(ClienteFornecedorDto dto)
    {
        int res = _appCliFor.Adicionar(dto);

        var settings = new JsonSerializerSettings();
        return Json(res, settings);
    }

The object dto is coming with the properties null

  • Confirms that the property names of the Customer object are the same as the Customer.

  • Yes they are the same, the only difference is the Id that is int, but I don’t think it interferes. And it’s a class that has inheritance and a method. I don’t know if it interferes.

  • Try as follows: var dto = $.toJSON(Customerssupplier);

  • This method is unique to a bilbiotace that I don’t have.. stringify is converting.. I tested it.

  • 1

    public Jsonresult Add([Frombody]Clientsupplier dto dto) and see if it works!

  • No. Now the object comes completely null

  • In addition to from body, add the [Httppost] before the controller declaration and post your DTO to see if it matches the object you’re posting... And when you say everything comes null, what came filled before?

  • Also add the Json that is being posted, you can capture by the same browser.

  • All that you told me (Httppost and Frombody) worked, but I had to adjust the ajax. data: JSON.stringify(ClienteFornecedor); It worked. Thank you very much

Show 4 more comments

1 answer

0

Good afternoon, could try doing the action as follows:

[HttpPost]
public JsonResult Adicionar([FromBody] ClienteFornecedorDto dto)

Browser other questions tagged

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