Error Sequence contains in elements asp.net

0

Good morning.

I'm trying to send a request in ajax in jquery, but it returns me that error. Sequence contains no elements, I'm trying to present data in the bootgrid

public ParametrosPaginacao(NameValueCollection dados)
        {

            string chave = dados.AllKeys.Where(k => k.StartsWith("asc")).First();

            string ordenacao = dados[chave];
            string campo = chave.Replace("sort[", String.Empty).Replace("]", String.Empty);

            CampoOrdenado = String.Format("{0} {1}", campo, ordenacao);

            Current = int.Parse(dados["current"]);
            RowCount = int.Parse(dados["RowCount"]);
            SearchPhrase = dados["searchPhrase"];

        }

        public int Current { get; set; }
        public int RowCount { get; set; }
        public string SearchPhrase { get; set; }
        public string CampoOrdenado { get; set; }

}

My ajax

$(document).on('click', '#contratos_vigentes', function () {

    var Status = $('#Status option:selected').text();
    var Gestor_Acc = $('#Gestor_Acc option:selected').text();
    var Gestor_Retida = $('#Gestor_Retida option:selected').text();
    $.ajax({
        url: 'FiltrarEPaginar',
        type: 'post',
        dataType: 'json',
        data: { status : Status, gestor_acc : Gestor_Acc, gestor_Retida : Gestor_Retida },

    });
    //window.location.href = "Contratos_Vigentes?status=" + Status + "&gestor_acc=" + Gestor_Acc + "&gestor_retida=" + Gestor_Retida;

});
                    
asked by anonymous 20.03.2018 / 14:34

1 answer

1

The error says that you tried to extract an element from a sequence, but it did not have any. The error probably comes from this line ( First () method )

string chave = dados.AllKeys.Where(k => k.StartsWith("asc")).First();

Therefore, it is necessary for your code to handle the case where there are no keys that begin with "asc".

Your ajax code does not send the parameter ParametersPaginacao , which should be an object, to the controller. Therefore, the ASP.NET MVC framework fails to execute the constructor. There are two options:

1 - Modify the constructor to handle the case where the data parameter is null or empty.

2 - Modify the javascript to mount and send the Parametrospaginacao object to the controller.

    
20.03.2018 / 14:43