javascript - 无法使用 Twitter Bootstrap Typeahead 发布 ID

标签 javascript asp.net-mvc twitter-bootstrap bootstrap-typeahead twitter-typeahead

我在我的 MVC5 项目中使用 Twitter Bootstrap Typeahead,它正确列出了相关记录。虽然我可以在更新程序部分检索所选记录的 Id 值,但我无法将其发布到表单提交上。我尝试了 Id 参数的许多不同组合,但没有任何意义。如何使用 Twitter Bootstrap Typeahead 发布 Id 参数?

查看:

@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>


Controller :

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);
}

最佳答案

将字段分为名称和 ID,您甚至可以将 ID 字段隐藏或只读。

<input id="StudentName" type="text" class="form-control tt-input" 
    autocomplete="off" spellcheck="false" dir="auto">

<input id="StudentId" name="StudentId" type="text">

<script>
    $("#StudentName").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);
            $("#StudentId").val(selectedId)
            return item;
        }
    });
</script>

关于javascript - 无法使用 Twitter Bootstrap Typeahead 发布 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680418/

相关文章:

javascript - 为什么 iframe 会因为 URL 中的特殊字符而加载失败?

html - 如何定位两个 <div>?一个来自 Bootstrap 模式的左侧,一个来自右侧?

html - 如何在悬停时从::before 标签中删除

javascript - ng-disabled 在 chrome 中不起作用

asp.net-mvc - Razor View 中的 MVC ASPX 服务器控件

javascript - 配置 webpack 忽略定义语句

javascript - Angular 6 : There are multiple modules with names that only differ in casing

javascript - 如果对象属性的状态检查并不总是存在会破坏该功能

javascript - asp.net mvc 显示 ViewBag 字典到 javascript

c# - MVC 2 RC 2 中的 IValueProvider