DB から名前のリストを取得する jQuery オートコンプリート テキスト ボックスを作成する必要があります。選択すると、ユーザーが適切なページ リンクに移動します。
この投稿のようなことをしようとしています: jQuery オートコンプリートの選択からコントローラー アクションを起動する
解決策は理にかなっており、クリックとリダイレクトは機能しますが、名前の文字列リスト以上のものを返す方法がわかりません。
現在のコントローラー コード (スニペット):
List<string> Names = new List<string>();
foreach (Child c in listfromDB)
{
Names.Add(c.Name);
//childNames.Add(new KeyValuePair<string, int>(c.Name, c.ID));
}
return Json(Names);
KeyValuePair
うまくいかなかったようです。代わりにオブジェクト配列を作成するにはどうすればよいですか?
私のjQueryコード:
$(document).ready(function () {
$("#find-child-box").autocomplete({
source: function (request, response) {
// define a function to call your Action (assuming UserController)
$.ajax({
url: '/Admin/AutoCompleteMyChildren', type: "POST", dataType: "json",
// query will be the param used by your action method
data: { query: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item, value: item };
}))
}
})
},
minLength: 1, // require at least one character from the user
select: function(event, ui) {
alert('mom');
window.location.href = ui.item.value;
}
});
});