I got some great help in this thread. But one problem remains, and Im struggling with it for a few hours now. For my autocomplete feature I want to use entries that are generated from my controller and not generate an array every time. I know how to normally include my controller as source for the autocomplete feature, thats no problem for me. But how would I include my controller path e.g. "/AutoCompleteFeatures/AutoCompleteSentenceSuggestion" in this fiddle? Everything should be the same, just that the source is no array but my controller path to deliever the values I need.
JS Fiddle
$(function () {
var availableTags = [
"ActionScript",
"AppleScript",
],
mL = 3;
function split(val) {
return val.split("\n");
}
function extractLast(term) {
return split(term).pop();
}
$("#SentenceSuggestionField1")
.autocomplete({
minLength: mL,
source: function (request, response) {
// delegate back to autocomplete, but extract the last term
var lastTerm = extractLast(request.term);
if (lastTerm.length >= mL) {
response($.ui.autocomplete.filter(availableTags, lastTerm));
}
},
select: function (event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push("\u2022 " + ui.item.value);
// Format value to display
terms.push("");
this.value = terms.join("\r\n");
return false;
}
});
});
`
Im struggling with this for a few hours now and tried many different things. But none of that worked yet, for example I tried to create an JS Array with values from database and use it like it in the fiddle, but that didnt work out. I tried several examples with my controller path as source, but that didnt work out too... So I hope you have a tip or hint for me, how to do this. Thank you!
As requested the controller action:
public JsonResult AutoCompleteField1(string term)
{
var data = db.SentenceSuggestions
.Where(s => s.SuggestionText.ToLower()
.Contains(term.ToLower()))
.Select(x => new { label = x.SuggestionText })
.ToArray();
return Json(data, JsonRequestBehavior.AllowGet);
}