如何在文本框中自动完成从数据库接收数据 [英] how do you do autocomplete in your text box receiving data from data base

查看:70
本文介绍了如何在文本框中自动完成从数据库接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我控制器上的方法



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> ;




}

解决方案

(function(){


(#txtSearch)。autocomplete({

source:'@ 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>




}


您的自动填充方法未完成添加以下代码,肯定会有效





 public JsonResult GetCompany(字符串术语)
{
var pa = new CompanyBusiness();
列表< 字符串 > namesList;
namesList = pa.GetAllCompanies()。ToList()。FindAll(x => x.CompanyName.ToLower()。StartsWith(term.ToLower()))。选择(y => y.CompanyName) .ToList();
返回Json(namesList,JsonRequestBehavior.AllowGet);
}









在你看来你必须在命名自动完成方法时调用您使用的名称。





 <   script     type   =  text / javascript  >  

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">


}

解决方案

(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">


}


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


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..


<script type="text/javascript">


这篇关于如何在文本框中自动完成从数据库接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆