Send data to controller via Json

Asked

Viewed 3,337 times

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:

Formulario

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; }

}
  • 1

    Have you tried checking your browser inspector to see how the request is being mounted?

  • 1

    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' : '' }]

  • 1

    Try sending the list direct instead of Sendersviewmodels: [listCnpj] send Sendersviewmodels: listCnpj

1 answer

2


I’ve had this problem, pass the parameter as a string JSON:

In your Ajax use this:

var dataJson = JSON.stringify(squirrel);

 $.ajax({
                type: 'POST',
                url: 'Gravar',
                data: { modelo: dataJson },
                dataType: 'json',
                success: function (data) {

                },
                error: function (data) {

                    alert('Error' + data);
                }
            });

Rename your method parameter in the p/ string controller:

 [HttpPost]
    public JsonResult Gravar(string modelo)
    {
        JsonResult json = new JsonResult();
        return json;
    }

That solution should work.

Browser other questions tagged

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