Hi I am trying to implement Auto Complete Text Box using "typeahead.js" something like http://sriniavatar.com/bootstrap-typeahead-in-asp-net-mvc/ in my MVC Project
My Script are bellow
<script type="text/javascript">
var substringMatcher = function (strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
matches = [];
substrRegex = new RegExp(q, 'i');
$.each(strs, function (i, str) {
if (substrRegex.employees(str)) {
matches.push(str);
}
});
cb(matches);
};
};
$.post('/Employee/TagSearch', null, function (data) {
var employees = data;
$('#sriniavatar').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'employees',
displayKey: 'value',
source: substringMatcher(employees)
});
});
And My Employee Controller's TagSearch Method is given bellow
[HttpPost]
public ActionResult TagSearch(string term)
{
DataAccess db = new DataAccess();
var employees = db.GetEmployeesName().ToList();
//if (!String.IsNullOrEmpty(term))
//{
// employees = employees.Where(e => e.Surname.Contains(term.Trim())
// || e.ForeName.Contains(term.Trim())
// || e.Win_UserName.Contains(term.Trim())
// ).ToList();
//}
return Json(employees, JsonRequestBehavior.AllowGet);
}
Now **Problem is that when I am running programme its showing following Error *"Unhandled exception at line 95, column 21 in http://localhost:57512/Employee/ViewEmployees
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'employees'"***
even though I have referenced following the scripts in my project
<link href="~/Content/typeahead.css" rel="stylesheet" /> <script "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script> <script src="~/Scripts/typeahead.js"></script> <br/>...Page Contents...<br>
<script src="~/Scripts/typeahead.mvc.model.js"></script>
@RenderSection("scripts", required: false)
I do not know of any JavaScript that has an employees method with regExp
if (substrRegex.employees(str)) {
^^^^^^^^^
I believe you want test
if (substrRegex.test(str)) {
User contributions licensed under CC BY-SA 3.0