I do a search in my database to search for product names without exceptions (I do not 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 typed name and repost 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'm 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>
In my cshtml page I have. My input field text
@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: link
But I did not succeed.