私はMVC5プロジェクトでTwitter Bootstrap Typeaheadを使用しており、関連するレコードを正しく一覧表示しています。私はupdaterセクションで選択したレコードのId値を取得することはできますが、フォーム送信時に投稿することはできません。 Idパラメータのさまざまな組み合わせを試しましたが、意味がありませんでした。 Twitter Bootstrap TypeaheadでIdパラメータを投稿するには?TwitterでポストできないBootstrap Typeahead
ビュー:
@Html.HiddenFor(m => m.StudentId)
<input id="StudentId" name="StudentId" type="text" class="form-control tt-input"
autocomplete="off" spellcheck="false" dir="auto">
<script>
$("#StudentId").typeahead({
minLength: 1,
source: function (query, process) {
var lookups = [];
map = {};
// This is going to make an HTTP post request to the controller
return $.post('/Grade/StudentLookup?query=%QUERY', { query: query }, function (data) {
// Loop through and push to the array
$.each(data, function (i, lookup) {
map[lookup.Name] = lookup;
lookups.push(lookup.Name);
});
// Process the details
process(lookups);
});
},
updater: function (item) {
var selectedId = map[item].Id;
console.log("Selected : " + selectedId);
return item;
}
});
</script>
コントローラ:名前とIDに
public ActionResult StudentLookup(string query)
{
var students = repository.Students.Select(m => new StudentViewModel
{
Id = m.Id,
Name = m.Name + " " + m.Surname
})
.Where(m => m.Name.StartsWith(query));
return Json(students, JsonRequestBehavior.AllowGet);
}
コンソールエラーとは何でしょうか?console.logのマップと項目は、アップデータ関数とソース関数のルックアップで確認できますか?あなたの問題は 'map'はグローバルなものではないと思います。 – MarZab
私はselectedIndexChangeでIdパラメータを "selectedId"として取得しますが、それをStudentIdモデル値に割り当てることはできません。ですから、唯一の問題はこれです。Model.StudentIdにどのように割り当てることができますか? –
ああ、隠しフィールド/別のフィールドを使用してそのIDを割り当てることをお勧めします。そうしないと、名前が予想された場所に数字が配置された理由が混乱します。 – MarZab