我正在尝试使用 bootstrap typeahead 但无法让它调用我的操作(ASP.NET MVC 4)。在浏览器中呈现以下内容:
<div class ="control-group">
<div class="controls">
<input autocomplete="off" data-provide="typeahead" data-url="/GetPets" id="Pets" type="text" />
</div>
</div>
<script type="text/javascript">
$(function () {
$('[data-provide=typeahead]').each(function () {
var self = $(this);
self.typeahead({
source: function (term, process) {
var url = self.data('url');
return $.getJSON(url, { term: term }, function (data) {
return process(data);
});
}
});
});
});
我的控制器方法看起来像
[HttpGet]
public JsonResult GetPets(string term)
{
// get data from DB. Here for simplicity sake I just make data up
var nameList = new List<string>
{
"Dog", "Cat", "Ant", "Alligator", "Beaver", "Camel", "Clam", "Dragonfly", "Goat"
};
var results = nameList.Where(n => n.ToLower().Contains(term.ToLower()));
return new JsonResult()
{
Data = results.ToArray(),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}