I have a table in the database called Disciplines, and I have a field search in my Index that if I type a letter "A" should suggest "Analysis and development" this is an example, but it does not work, apparently the code is right but is not suggesting anything when I type a letter.
Model discipline Method I use to list the disciplines:
public static List<Disciplina> listaDisciplina()
{
SqlConnection conexao = new SqlConnection(ConfigurationManager.ConnectionStrings["minhaConexao"].ConnectionString);
conexao.Open();
string select = "SELECT * FROM Disciplina ";
SqlCommand selectComando = new SqlCommand(select, conexao);
SqlDataReader sqlReader = selectComando.ExecuteReader();
List<Disciplina> DisciplianadeAlunos = new List<Disciplina>();
while (sqlReader.Read())
{
DisciplianadeAlunos.Add(new Disciplina(int.Parse(sqlReader["idDisciplina"].ToString()),
sqlReader["NomeDisciplina"].ToString(),
sqlReader["Semestre"].ToString(),
sqlReader["Curso"].ToString()));// fim add
}
return DisciplianadeAlunos;
}
My method that I use in auto complete reuses my list of Disciplines, bringing a list of disciplines:
public static List<String> buscaDisciplina(string term)
{
return listaDisciplina()
.Where(p => p.NomeDisciplina.StartsWith(term, StringComparison.OrdinalIgnoreCase))
.Select(p => p.NomeDisciplina).ToList();
}
My Controller Disciplines:
public ActionResult buscaDisciplina(string term)
{
return Json(Disciplina.buscaDisciplina(term), JsonRequestBehavior.AllowGet);
}
My index
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.12.4.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.js"></script>
<script>
$(function () {
$("#campoBusca").autocomplete({
source: '@Url.Action("buscaDisciplina", "Disciplina")'
});
});
</script>
My search field inside the nav bar
<form class="navbar-form navbar-left" role="search">
<input class="form-control mr-sm-2" id="campoBusca" type="text" placeholder="Buscar">
@*<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>*@
</form>