问题描述
这是我控制器上的方法
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 =找不到;
}
int pageSize = 4;
int pageNumber =(页面? 1);
返回视图(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(字符串术语)
{
var pa = new CompanyBusiness();
List< string> namesList;
namesList = pa.GetAllCompanies()。ToList()。FindAll(x => x.CompanyName.ToLower()。StartsWith(term.ToLower()))。选择(y = > y.CompanyName)
}
这就是我的观点的样子...
@model System.Collections.Generic.IEnumerable< template.model.model.documentsview>
@using(Html.BeginForm(Index,Company,FormMethod 。获得))
{
@section scripts {
< script type =text / javascript>
$(function( ){
$(#txtSearch)。自动完成({
来源:'@ Url.Action(公司)'
});
});
< / script>
}
搜索: | @ Html.TextBox(SearchString,ViewBag.CurrentFilter as string, null,new {@class =form-control,placeholder =Company Name}) | < input type =submitvalue =Searchclass =btn btn-primary> ; |
}
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"> |
}
这篇关于如何在文本框中自动完成从数据库接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!