Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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">


}
Posted

1 solution

your autocomplete method is not finished add the following code in it, surely it will work


XML
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).ToList();
           return Json(namesList, JsonRequestBehavior.AllowGet);
       }




and on your view you must call the name you use when naming your autocomplete method like this..


XML
<script type="text/javascript">
            $(function () {
                $("#txtSearch").autocomplete({
                    source: '@Url.Action("GetCompany")'


                });
            });
        </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900