コントローラ
[HttpGet]
public JsonResult GetTags()
{
var data = entity.Tags.Select(x => new { tagName = x.TagName });
return Json(new { result = data }, JsonRequestBehavior.AllowGet);
}
脚本
$(function () {
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#tags")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("autocomplete").menu.active) {
event.preventDefault();
}
})
.autocomplete({
source: function (request, response) {
$.getJSON('@Url.Action("GetTags", "Question")', {
term: extractLast(request.term)
}, response);
},
search: function () {
// custom minLength
var term = extractLast(this.value);
if (term.length < 2) {
return false;
}
},
focus: function () {
// prevent value inserted on focus
return false;
},
select: function (event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
});
実際、スクリプトはデフォルトの jquery-ui-autocomplete-with-multi-values からのものです
かみそり
<div class="demo">
<div class="ui-widget">
@Html.TextBoxFor(x => x.Title, new { @class = "my_text_box", id = "tags" })
</div>
</div>
コントローラー アクションがトリガーされますが、テキスト ボックスにデータが表示されません。JavaScript をデバッグしましたが、関数がトリガーされません。どうすれば修正できますか?
auto-complete-with-multi-values
この方法ではなく、別の方法で行うことができます。