アップデート
jQueryUIオートコンプリートを紹介するサンプルプロジェクトをGitHubhttps://github.com/alfalfastrange/jQueryAutocompleteSampleのテキストボックスに投稿しました
。
私は通常のMVCTextBoxでそれを使用します
@Html.TextBoxFor(model => model.MainBranch, new {id = "SearchField", @class = "ui-widget TextField_220" })
これが私のAjax通話のクリップです
最初に、検索対象のアイテムの内部キャッシュをチェックします。見つからない場合は、コントローラーアクションへのAjaxリクエストを実行して、一致するレコードを取得します。
$("#SearchField").autocomplete({
source: function (request, response) {
var term = request.term;
if (term in entityCache) {
response(entityCache[term]);
return;
}
if (entitiesXhr != null) {
entitiesXhr.abort();
}
$.ajax({
url: actionUrl,
data: request,
type: "GET",
contentType: "application/json; charset=utf-8",
timeout: 10000,
dataType: "json",
success: function (data) {
entityCache[term] = term;
response($.map(data, function (item) {
return { label: item.SchoolName, value: item.EntityName, id: item.EntityID, code: item.EntityCode };
}));
}
});
},
minLength: 3,
select: function (event, result) {
var id = result.item.id;
var code = result.item.code;
getEntityXhr(id, code);
}
});
これがすべてのコードではありませんが、ここでキャッシュがどのように検索され、Ajax呼び出しが行われ、応答で何が行われるかを確認できるはずです。select
選択した値で何かできるようにセクションがあります