i have a textbox and i want to make an autocomplete feature
i have this view i add the libraries but it does not respond at all
<link href="~/Content/themes/base/autocomplete.css" rel="stylesheet" />
<link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-3.1.1.js"></script>
<input type="text" id="q" name="q" placeholder="Enter the text please" />
the javascript code is this
$(document).ready(function () {
$("#q").autocomplete({
source: function (request, response) {
$.ajax({
url: '/Book/AutoComplete/',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data, function (item) {
return { label: item.Name, value: item.Name };
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
});
and here's the controller
public JsonResult AutoComplete(string prefix)
{
var book = db.Book.Where(x => x.Name.Contains(prefix)).Select(x =>x.Name).ToList();
return Json(book, JsonRequestBehavior.AllowGet);
}