Error making autocomplete c#

Asked

Viewed 45 times

0

I’m trying to make an autocomplete, but is giving the following error, not even make the post...

inserir a descrição da imagem aqui

My code is like this:

 <script type="text/javascript">
$(function () {
    $("[id$=DsValor]").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '@Url.Action("GetPalavraChave", "HomeCanal")',
                data: "{ 'prefixo': '" + request.term + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            label: item.split('-')[0],
                            val: item.split('-')[1]
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        select: function (e, i) {
            $("[id$=hfCustomerId]").val(i.item.val);
        },
        minLength: 1
    });
});

And the backend is like this:

 [WebMethod]
    public static string[] GetPalavraChave(string prefixo)
    {
        List<string> clientes = new List<string>();
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["WhiteLabelVOD"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "SELECT * FROM Palavra_Chave WHERE Ic_Ativo = 1 AND Ds_Palavra_Chave LIKE @Texto + '%'";
                cmd.Parameters.AddWithValue("@Texto", prefixo);
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        clientes.Add(string.Format("{0}-{1}", sdr["ContactName"], sdr["CustomerId"]));
                    }
                }
                conn.Close();
            }
        }
        return clientes.ToArray();
    }
  • You have a mistake in jquery-ui.theme.css, Did you alter that file? And in order to help we need to see more of your front only the autocomplete code doesn’t help, because the second error isn’t related to it either

1 answer

0


I managed to solve as follows, returning in Json!

   [HttpPost]
    public JsonResult AutoCompleteTag(string prefixo)
    {
        List<PalavraChaveModel> palavras = new List<PalavraChaveModel>();
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["WhiteLabelVOD"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "SELECT * FROM Palavra_Chave WHERE Ic_Ativo = 1 AND Ds_Palavra_Chave LIKE @Texto + '%'";
                cmd.Parameters.AddWithValue("@Texto", prefixo);
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        PalavraChaveModel pl = new PalavraChaveModel();
                        pl.IdPalavraChave = (Int32)sdr["Id_Palavra_Chave"];
                        pl.DsPalavraChave = (string)sdr["Ds_Palavra_Chave"];
                        palavras.Add(pl);
                    }
                }
                conn.Close();
            }
        }
        return Json(palavras, JsonRequestBehavior.AllowGet);
    }

And my view went like this:

  $("[id$=DsValor]").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '@Url.Action("AutoCompleteTag", "HomeCanal")',
                        data: "{'prefixo':'" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.DsPalavraChave, value: item.DsPalavraChave, IdPalavraChave: item.IdPalavraChave };
                            }))

                        },
                        error: function (response) {
                            alert(response.responseText);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        }
                    });
                },
                select: function (e, i) {
                    $("#IdPalavraChave").val(i.item.IdPalavraChave);
                },
                minLength: 1
            });

Browser other questions tagged

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