0

I´m trying to get a list with Ajax when a modal shows up, but for some reason my method always receives a null variable. I´m sure that the issue is on the Ajax call, because if I test my method using 1 as an argument instead of the value from Ajax, it returns the list the way I wanted.

JS:

$("#visualizacao").on('show.bs.modal', function (e) {
            var data = $(e.relatedTarget);
            var idAviso = data.context.dataset.avisoid;
                $.ajax({
                    type: 'GET',
                    url: 'ListaVisuAviso/' +idAviso,
                    success: function (response) {
                        $('#visu-table tbody').empty(); 
                        var trHTML = '';
                        $.each(response, function (i, item) {
                            trHTML += '<tr><td>' + item.NOME + '</td><td>' + item.DATA_HORA + '</td><td>' + item.DEPARTAMENTO + '</td></tr>';
                        });
                        $('#visu-table tbody').append(trHTML);
                        $('#modal-visu').modal('show');
                    },
                    error: function (xhr) {
                        console.log(xhr);
                    }
                });
            $('#modalAviso').modal('show');
        });

C#

    [HttpGet]
    public JsonResult ListaVisuAviso(string avisoId)
    {
        //var avisoid = 1;
        var avisoid = Convert.ToDecimal(avisoId);

        var query =
            from a in _dataContext.TB_AVISOS_NOTIFICACOES
            join b in _dataContext.VW_USUARIOS4 on a.USUARIO_PR equals b.USUARIOID
            where a.AVISO_ID == avisoid
            select new VisuAviso()
            {
                NOME = b.NOME,
                DATA_HORA = a.DATA_HORA.ToString(),
                DEPARTAMENTO = b.DEPARTAMENTO
            };
        return Json(query, JsonRequestBehavior.AllowGet);
    }
Jerdine Sabio
  • 5,688
  • 2
  • 11
  • 23
lufer88x
  • 11
  • 3

1 Answers1

1

I discovered what was causing the "issue". To use this way of sending the parameter, my route config on the backend was expecting it to be a parameter called "id". So I would either change my receiving parameter to "id" instead of "avisoId" like the following:

    [HttpGet]
    public JsonResult ListaVisuAviso(string id)
    {
        //var avisoid = 4;
        var avisoid = Convert.ToDecimal(id);

        var query =
            from a in _dataContext.TB_AVISOS_NOTIFICACOES
            join b in _dataContext.VW_USUARIOS4 on a.USUARIO_PR equals b.USUARIOID
            where a.AVISO_ID == avisoid
            select new VisuAviso()
            {
                NOME = b.NOME,
                DATA_HORA = a.DATA_HORA.ToString(),
                DEPARTAMENTO = b.DEPARTAMENTO
            };
        return Json(query, JsonRequestBehavior.AllowGet);

Or, I would have do specify the name of the parameter on the JS, like this "?usuarioId=", this way the route would know that it´s not the id parameter:

        $("#visualizacao").on('show.bs.modal', function (e) {
            var idAviso = $(e.relatedTarget).attr('data-avisoid');
                $.ajax({
                    type: 'GET',
                    url: 'ListaVisuAviso/?usuarioId =' + idAviso,
                    dataType: 'json',
                    success: function (response) {
                        $('#visu-table tbody').empty(); 
                        var trHTML = '';
                        $.each(response, function (i, item) {
                            trHTML += '<tr><td>' + item.NOME + '</td><td>' + item.DATA_HORA + '</td><td>' + item.DEPARTAMENTO + '</td></tr>';
                        });
                        $('#visu-table tbody').append(trHTML);
                        $('#modal-visu').modal('show');
                    },
                    error: function (xhr) {
                        console.log(xhr);
                    }
                });
            $('#modalAviso').modal('show');
        });
lufer88x
  • 11
  • 3