this is the method I have on my controller
public ActionResult Index(string searchstring, string currentFilter, int? page)
{
if (searchstring != null)
{
page = 1;
}
else
{
searchstring = currentFilter;
}
ViewBag.CurrentFilter = searchstring;
var searchh = _db.GetAllCompanies();
if (!string.IsNullOrEmpty(searchstring))
{
searchh = SearchByName_ByCompanyName(searchstring);
}
else
{
ViewBag.found = "not found";
}
int pageSize = 4;
int pageNumber = (page ?? 1);
return View(searchh.ToPagedList(pageNumber, pageSize));
}
private List<companyviewmodel> SearchByName_ByCompanyName(string searchstring)
{
return (_db.GetAllCompanies().Where(p => p.CompanyName.ToLower().Contains(searchstring.ToLower())).ToList());
}
public JsonResult GetCompany(string term)
{
var pa=new CompanyBusiness();
List<string> namesList;
namesList = pa.GetAllCompanies().ToList().FindAll(x => x.CompanyName.ToLower().StartsWith(term.ToLower())).Select(y => y.CompanyName)
}
and this is how my view looks like...
@model System.Collections.Generic.IEnumerable<template.model.model.documentsview>
@using (Html.BeginForm("Index", "Company", FormMethod.Get))
{
@section scripts{
<script type="text/javascript">
$(function () {
$("#txtSearch").autocomplete({
source: '@Url.Action("Company")'
});
});
</script>
}
Search: | @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, null, new { @class = "form-control", placeholder = "Company Name" }) | <input type="submit" value="Search" class="btn btn-primary"> |
}