0
Good morning guys, I’m having trouble implementing a field with autocomplete in an Asp . Net MVC application. The problem is this, using an Ajax I can go to my controller and search for results of the query, however, in the return where to display the result it always falls in the "error" field, showing Json in an "Alert" as I will show below:
$(document).ready(function () {
$('#Cd_Cod_Produto').keypress(function () {
$('#Cd_Cod_Produto').autocomplete({
source: function (request, response) {
$.ajax({
url: "/Estoque/GetAutoComplete",
type: "POST",
dataType: "application/json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return {
label: item.Name,
value: item.Value
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
}
});
});
});
As you can see, if an error occurs, an Alert is displayed with the search result, and always falls into this Alert. My Controller is like this:
public ActionResult GetAutoComplete(string term)
{
EstoqueDAO dao = new EstoqueDAO();
List<Autocomplete> lista = dao.BuscaResultados(term);
dao.Dispose();
string json = JsonConvert.SerializeObject(lista);
return Json(json, JsonRequestBehavior.AllowGet);
}
and this is the model I’m using only for testing:
public class Autocomplete
{
public string Name { get; set; }
public string Value { get; set; }
}
The database search is returning values correctly, I believe that is not the problem.
As you can see in the image is what happens with the return of the function. Does anyone have any hint or hint of where I’m going wrong? Thanks for your help.
Sincerely yours,
Updating:
I changed the script to this shape:
$(document).ready(function () {
$('#Cd_Cod_Produto').keypress(function () {
$('#Cd_Cod_Produto').autocomplete({
source: function () {
$.get(url, { term: $('#Cd_Cod_Produto').val() }, function (data) {
$.map(data, function (item) {
return {
label: item.Name,
val: item.Value
//console.log(item.Name);
}
});
});
}
});
});
});
This way it looks for the results in the controller, but has not yet shown anything in the autocomplete. If you remove the comments from the "/console.log(item.Name)" line it normally displays the results on the console.
Take out that code snippet
string json = JsonConvert.SerializeObject(lista);
and returns like this:return Json(lista, JsonRequestBehavior.AllowGet);
– Pedro Paulo
Hi @Pedropaulo, thanks for replying. I did what you said, but it keeps falling on Alert, the difference is that the returned Json looks like this: [{"Name":"ES001","Value":"ES001"},{"Name":"ES002","Value":"ES002"},{"Name":"ES003","Value":"ES003"},{"Name":"ES004","Value":"ES004"}]...without the """""" as it was being shown earlier.
– Q.Wesley
Another observation, this method should be
GET
, semantically makes more sense. Even the name of your back-end method starts withGet
.– Pedro Paulo
@Peter Paul, I changed the methods to become more semantic, but the problem remains.
– Q.Wesley