Pass array parameter by HTTPGET in ajax to Actionresult

Asked

Viewed 161 times

0

Hello.

I have an Ajax Httpget function that would need to take an array to an Actionresult. How do I do this?

$.ajax({
        url: 'AtualizarTabelaDeRelatorio',
        data: {
            arrayDeInstalacao: arrayDeInstalacao,
            arrayDePendencias: arrayDePendencias,
            parametroDoNumeroDaPagina: pagina

        },
        cache: false,
        success: function (data) {
            $("#dvResults").html(data);
            $('body').css('cursor', 'default');
        }
    });

In C#:

[HttpGet]
    public ActionResult AtualizarTabelaDeRelatorioDeQuantitativoDePendencia(int[] arrayDeInstalacao= null, int[] arrayDePendencias= null, int? parametroDoNumeroDaPagina = null, int? parametroDoTamanhoDaPagina = null)
    {}

but it’s not working! Obviously because it’s httpget. There’s no way at all?

  • 1

    but the names in the object data do not match the method signature, should be data: {
 'arrayDeInstalacao': arrayDeInstalacao, .... etc

  • fixed, but that’s not the problem, I had only changed when I created the post, the names in the project are the same...

  • Displays some error?

  • 1

    @Rafaelamarraschi See in the browser console which URL is being used in the request, it is probably wrong.

1 answer

1


Rafaela, I did an example in ASP.NET CORE.

In your code the URL does not match the Controller you entered. An important point is to correctly define the arrays in Javascript and inform the following parameter in jQuery: Traditional = true. According to the jQuery documentation, if set to true, it enables standard encoding. Some frameworks "don’t understand standard encoding".

Example:

Traditional = false (Default behavior)

a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3

Traditional = true

a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3

Code below...

    [HttpGet]
    public ActionResult AtualizarTabelaDeRelatorioDeQuantitativoDePendencia(int[] arrayDeInstalacao = null, 
                                                                            int[] arrayDePendencias = null, 
                                                                            int? parametroDoNumeroDaPagina = null, 
                                                                            int? parametroDoTamanhoDaPagina = null)
    {
        return Json(arrayDeInstalacao);
    }

<div class="row">

<button id="btnEnviar" class="btn btn-primary">Enviar</button>

@section Scripts{ 
<script>
    $('#btnEnviar').on('click', function () {

        var arrayDeInstalacaoForm = [100, 200, 300];
        var arrayDePendenciasForm = [1, 2, 3];
        var parametroDoNumeroDaPaginaForm = 90; 
        var parametroDoTamanhoDaPaginaForm = 50;

        var request = $.ajax({
            method: "GET",
            url: '@Url.Action("AtualizarTabelaDeRelatorioDeQuantitativoDePendencia", "Home")', // Parâmetros - Action e Controller
            contentType: "application/json; charset=utf-8",
            cache: false, 
            traditional: true, 
            dataType: "json",
            data: {
                arrayDeInstalacao: arrayDeInstalacaoForm,
                arrayDePendencias: arrayDePendenciasForm,
                parametroDoNumeroDaPagina: parametroDoNumeroDaPaginaForm,
                parametroDoTamanhoDaPagina: parametroDoTamanhoDaPaginaForm
            }
        }); 

        //Se a requisição for feita com sucesso...
        request.done(function (msg) {
            alert(msg)
        });

        //Se houver falha na requisição
        request.fail(function (jqXHR, textStatus) {
            alert(textStatus)
        });
    })
</script>

}

  • mto thanks for the help! In this case it returns a json, right? Because I would need an actionresult msm

  • Just put a Re-turn View()

Browser other questions tagged

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