Auto complete does not work Asp.net mvc

Asked

Viewed 131 times

0

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" that’s an example, but it doesn’t work, apparently the code is right but is not suggesting anything when I type a letter.

Model discipline Method I use to list subjects:

        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 listDisciplines, 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>
  • 1

    You put a break point in your controller action to see if the request is hitting inside?

  • I put, and worse q when I type a letter not even call my action =/

  • Right, so the problem is probably in the url. Try instead '@Url.Action("buscaDisciplina", "Disciplina")' put '/Namedeontroller/Nameseatation', take the test and tell me.

  • No effect, I made an element inspect, but dis "Uncaught Typeerror: $(...). autocomplete is not a Function". problem with bank is not pq all other things are pulling right, except my auto complete

  • In this case the autocomplete is null. You added the jquery autocomplete library?

  • this resolved stopped error related jquery I just added the blibiotecas straight into the Bundle, but even so it will probably not some detail is not letting call the auto complete, but thanks if I discover I warning.

  • I found problem missing , "~/Scripts/jquery-ui-1.12. 1.min. js and this "~/Content/themes/base/jquery-ui.min.css" is now right

Show 2 more comments
No answers

Browser other questions tagged

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