C # function in the controller to receive an array

0

I need to get an array of select and send to a function in the controller, but it always arrives as null, even sending the complete array. Here is the code: Controller:

[AcceptVerbs(HttpVerbs.Get)]
    public ActionResult GetLocais(int [] idcTipoLocal)
    {
        var local = context.Locais.Where(x => x.IdcSite == ContextSession.UsuarioLogado.IdcSite && x.Status /*&& x.IdcTipoLocal == idcTipoLocal*/).Distinct()
               .Select(x => new { id = x.IdcLocal, text = x.Nome }).OrderBy(y => y.text).ToList();
        return Json(local, JsonRequestBehavior.AllowGet);
    }

JavaScript:

 var opt = new Array();
$("#selTipLocal").change(function () {
    var obj = [];
    $('#selTipLocal > option:selected').each(
        function (i) {
            opt[i] = $(this).val();
            opt[i] = parseInt(opt[i]);
        });

    var jsonKey = {
        "idcTipoLocal": opt
    };
    $.get("/relocorrencia/getlocais", jsonKey, function (data) {
        $("#selLocal").multiselect("destroy");
        multiSelectRelOco("selLocal", data);
    });
});

I already checked and the arrey arrives ok, but when sending to the controler, idctipolocal always arrives as null

    
asked by anonymous 28.03.2018 / 22:18

1 answer

0

I was able to do it this way:

var opt = new Array ();

$("#selTipLocal").change(function () {

    $('#selTipLocal > option:selected').each(
        function (i) {
            opt[i] = $(this).val();
            opt[i] = parseInt(opt[i]);
        }
    )
    if (opt[0] != null) {
        var obj = [];
        $.ajax({
            cache: false,
            traditional: true,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            type: "GET",
            data: { "idcTipoLocal": opt },
            url: "/relocorrencia/getlocais",
            success: function (data) {
                $.each(data, function (i, obj) {
                    $("#selLocal").multiselect("destroy");
                    multiSelectRelOco("selLocal", data);
                });
            }
        });
    }
    else {
        multiSelectRelOco("selLocal", null);
    }
});
    
29.03.2018 / 22:30