I am trying to write an ajax user search.I have this text field:
<input type="text" id="userSearch" class="search-query" placeholder="Search">
And this jQuery for it in a .js
file:
$("#userSearch").keyup(function () {
if ($(this).val().length >= 3) {
$.getJSON('/User/SearchUsers', { displayName: $(this).val() }, function (data) {
if (data == null) {
alert("nothing");
}
else{
alert("OK");
}
});
}
});
Here is the UserController
and Application
and Repository
Classes:
[HttpGet]
public ActionResult SearchUsers(string displayName)
{
return Json(userApp.GetUserBySearch(displayName), JsonRequestBehavior.AllowGet);
}
public List<User> GetUserBySearch(string displayName)
{
return userRepo.GetUserBySearch(displayName);
}
public List<User> GetUserBySearch(string displayName)
{
return context.Users.Include("Group").Where(u => u.DisplayName.Contains(displayName)).ToList();
}
But this is not working at all.It doesnt alert at all.So what does it mean?It means that data is null and is not null?! I have tried this too:
var i = 0;
for (i = 0; i <= data.length; i++) {
alert(data[i].UserId);
}
But It is not working too. I have do something like this for thousand of times but now IT IS NOT WORKING... What shoud I do...?