Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Suggestion autocomplete Textbox MVC and Javascript

Hi to all and thanks for your help,

I have a text with autocomplete suggestions in MVC.

The textbox in the view is:

   <form action="Locator">
      <div class="col-lg-3 col-md-3 hidden-sm hidden-xs" style="margin-top:2%;">
         <input type="text" class="formLocator" value="Milano" data-date-end-date="0d" id="textLocator" name="searchstring">
         <h4 class="FormTextLocator">dove</h4>
      </div>
   </form>

I have created a javascript code for this textbox:

$(document).ready(function () {
    $('[data-toggle="tooltip"]').tooltip();
    // Locator
    $("#textLocator").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Home/AutocompleteSuggestions",
                type: "POST",
                dataType: "json",
                data: { term: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { value: item.name };
                    }))
                }
            })
        },
        messages: {
            noResults: "", results: ""
        }
    });
});

And the controller associated is:

[HttpPost]
public JsonResult AutocompleteSuggestions(string term)
{
   var db = new TocFruit();
   var suggestions = from s in db.city select s.name;
   var namelist = suggestions.Where(n => n.ToString().ToLower().StartsWith(term.ToLower()));
   return Json(namelist, JsonRequestBehavior.AllowGet);
}

The autocomplete value are passed to view but this is the result view:

enter image description here

Please help me, i really don't know what to do.

Thanks to all,

Roberto

like image 644
user5297740 Avatar asked Jul 12 '26 11:07

user5297740


1 Answers

I think you forget to mention label in Autocomplete

$(document).ready(function () {
$('[data-toggle="tooltip"]').tooltip();
// Locator
$("#textLocator").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Home/AutocompleteSuggestions",
            type: "POST",
            dataType: "json",
            data: { term: request.term },
            success: function (data) {
                response($.map(data, function (item) {
                    return { 
                             value: item.name,
                             label : item.name
                           };
                }))
            }
        })
    },
    messages: {
        noResults: "", results: ""
    }
});

});

like image 70
Ubiquitous Developers Avatar answered Jul 14 '26 01:07

Ubiquitous Developers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!