控制器
[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
用另一种方式,而不是这种方式。