7
I am trying to send via Ajax some data from my form. In this form, I have fields inputs
normal and a list of vendors I previously selected via checkbox. Follow screen:
I am creating the JSON object and sending by Ajax, but my list is null on controller.
Ajax:
var nomeUsuario = $("#NomeUsuario").val();
var sobrenomeUsuario = $("#SobrenomeUsuario").val();
var codigoUsuario = $("#CodigoUsuario").val();
var emailUsuario = $("#EmailUsuario").val();
//Busca todos os fornecedores selecionados
var listCnpj = [];
$('#datagrid tbody tr').filter(':has(:checkbox:checked)').each(function () {
var elem = $(this)[0];
listCnpj.push("Codigocnpj:" + elem.cells[0].innerHTML);
});
// build json object
var squirrel = {
NomeUsuario: nomeUsuario,
SobrenomeUsuario: sobrenomeUsuario,
CodigoUsuario: codigoUsuario,
EmailUsuario: emailUsuario,
ListaFornecedorViewModels: [listCnpj]
};
$.ajax({
type: 'POST',
url: 'Gravar',
data: squirrel,
dataType: 'json',
success: function (data) {
},
error: function (data) {
alert('Error' + data);
}
});
Controller:
[HttpPost]
public JsonResult Gravar(UsuarioViewModel usuarioViewModel)
{
JsonResult json = new JsonResult();
return json;
}
And my model:
public class UsuarioViewModel {
public int Id { get; set; }
public string CodigoUsuario { get; set; }
public string NomeUsuario { get; set; }
public string SobrenomeUsuario { get; set; }
public string SenhaUsuario { get; set; }
public string EmailUsuario { get; set; }
public int Status { get; set; }
public string Prontuario { get; set; }
public string FornecedorBusca { get; set; }
public List<Fornecedor> ListaFornecedorViewModels { get; set; }
}
Have you tried checking your browser inspector to see how the request is being mounted?
– Leonel Sanches da Silva
Ja yes Gypsy... Going with the data: The list looks like this: listCnpj = ["{'Codigocnpj' : '85.558.272/0001-40','Razaosocial' : '', 'Municipio' : '' }", "{'Codigocnpj' : '85.558.272/0001-70','Razaosocial' : '' , 'Municipio' : '' }]
– Luciano Carlos
Try sending the list direct instead of Sendersviewmodels: [listCnpj] send Sendersviewmodels: listCnpj
– Brunno