returning blank autocomplete

Asked

Viewed 77 times

0

I do a search in my database to find the name of the products without exceptions (I don’t know if it’s relevant, but this return is a string list)

var listaDosProdutos = _servico.ObterNomeDosProdutos();

then I take this list and do a search of which product starts with the name typed and reptorno in json format

var listaFiltroProdutos = listaDosProdutos.Where(str => str.ToLower().StartsWith(Prefix.ToLower())).ToList();

controller code:

[HttpPost]
public JsonResult ConsultaProduct(string Prefix)
{
    //Searching records from list using LINQ query
    var listaDosProdutos = _servico.ObterNomeDosProdutos();
    var listaFiltroProdutos = listaDosProdutos.Where(str => str.ToLower().StartsWith(Prefix.ToLower())).ToList();

    return Json(listaFiltroProdutos);
}

Inside my _Layout layer I am loading two Jquery Cdn

<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.3.1.min.js"
        asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
        asp-fallback-test="window.jQuery"
        crossorigin="anonymous"
        integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT">
</script>

and

 <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
            integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
            crossorigin="anonymous"></script>

On my cshtml page I have. My input text field

@using (Html.BeginForm("Index", "Product", FormMethod.Get, new { @class = "navbar-form navbar-left" }))
{
          <input type="text" class="form-control focus" id="consultarProduto" name="consultarProduto" placeholder="Search by Name, CasNo, Class, Group Name ...">
          <button type="submit" class="btn btn-default">Submit</button>
}

and my js

$("#consultarProduto").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/Product/ConsultaProduct",
                    type: "POST",
                    dataType: "json",
                    data: { Prefix: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.CommercialName, value: item.CommercialName };
                        }))
                    }
                })
            }
        });

I did some tests with other codes: https://stackoverflow.com/questions/48525711/asp-net-core-jquery-autocomplete-returns-blank-lines-in-the-list

But I was unsuccessful.

  • Leandro, Victor even put a debbuger in the Success of your Ajax? (I would like to make a comment). If yes, the return of your list is being returned correctly by your request?

  • It’s returning an Array with 5 data, which is the data I hoped it would pick up, but I don’t know if it’s returning to my request as I do?

1 answer

0

I found the error, here in my code I’m returning a string list (I’m not sure) and was trying to access this string with:

return { label: item.CommercialName, value: item.CommercialName };

What I did to resolve was just return the item this way:

return item;

Browser other questions tagged

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