getJSON with parameter

-2

I'm running a query with getJSON calling the MVC controler and can return a JsonResult. When I pass a letter to search for the first time the controller is triggered and returns the value filtered by the infomed letter, but if I delete that letter from the field and type it again, the controller is no longer triggered and getJSON somehow stored the first query and displays the result returned from the first query again.

Follow the snippet.

$.getJSON("/Funcionalidade/FindWithByTelaTop", { Id_Sistema: id_sistema, Id_Grupo: $("#GrupoIdUpd").val(), Filtro: filtro, Top: 7 }, function (data) {
                    $.each(data.Records, function (i, item) {

                        TblTelaAddRow(item);
                        hasRow = true;
                    });

                    if (hasRow == false) {
                        if ($("#spanMessage").html().length == 0) {
                            $("#spanMessage").html("A consulta não retornou resultados");
                        }
                    } else {
                        if ($("#spanMessage").html().length > 0) {
                            $("#spanMessage").html("");
                        }
                    }
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus + " - " + errorThrown);
                });

Controller

public JsonResult FindWithByTelaTop(int Id_Sistema, int Id_Grupo, string Filtro = "", int Top = 10)
        {
            try
            {
                var dados = _funcionalidadeModels.find(Id_Sistema, Id_Grupo, Filtro)               
                    .Select(x => new
                    {
                        x.Id,
                        x.Tela                        
                    }).AsParallel();

                if (dados != null)
                {
                    return Json(new
                    {
                        Result = "OK",
                        Records = dados.OrderBy(o => o.Tela).Take(Top),
                        TotalRecordCount = dados.Count()
                    }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    string msg = "Erro ao tentar obter lista. Objeto nulo.";
                    return Json(new { Result = "ERROR", Records = msg }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                string msg = "Erro ao tentar consultar lista de clientes. Info: " + ex.Message;
                if (ex.InnerException != null)
                {
                    msg += ". Exceção interna: " + ex.InnerException.Message;
                }
                return Json(new { Result = "ERROR", Records = msg }, JsonRequestBehavior.AllowGet);
            }
        }
    
asked by anonymous 28.03.2016 / 22:33

1 answer

0

Hello

Can solve this problem. Find out after many searches that some browsers cache these filters, and to work around the problem you need to do the following.

If you are using $.ajax you must include cache = false and for $.getJSON a new Date().getTime() parameter must be included. This will always cause the query to be passed to the controller forcing the browser to perform the search.

    
29.03.2016 / 15:43