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.
– Jr. Pacheco
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.– SM_S
Try as follows: var dto = $.toJSON(Customerssupplier);
– Jr. Pacheco
This method is unique to a bilbiotace that I don’t have.. stringify is converting.. I tested it.
– SM_S
public Jsonresult Add([Frombody]Clientsupplier dto dto) and see if it works!
– Jr. Pacheco
No. Now the object comes completely
null– SM_S
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?
– Leandro Angelo
Also add the Json that is being posted, you can capture by the same browser.
– Leandro Angelo
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– SM_S