我想要的客戶名單上實現搜索功能,該功能將在本教程的Asp.Net網站的詳細,Asp.Net MVC3添加搜索功能
在我的控制我有以下
public ViewResult Index(string sortOrder, string searchString)
{
ViewBag.CustomerNameSortParm = String.IsNullOrEmpty(sortOrder) ? "CustomerName desc" : "";
ViewBag.PrimaryContactNameSortParm = sortOrder == "PrimaryContactName" ? "PrimaryContactName desc" : "PrimaryContactName";
var cust = repository.Customers;
if (!String.IsNullOrEmpty(searchString))
{
cust = cust.Where(c => c.CustomerName.ToUpper().Contains(searchString.ToUpper())
|| c.PrimaryContactName.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "CustomerName desc":
cust = repository.Customers.OrderByDescending(s => s.CustomerName);
break;
case "PrimaryContactName":
cust = repository.Customers.OrderBy(s => s.PrimaryContactName);
break;
case "PrimaryContactName desc":
cust = repository.Customers.OrderByDescending(s => s.PrimaryContactName);
break;
default:
cust = repository.Customers.OrderBy(s => s.CustomerName);
break;
}
return View(cust.ToList());
}
if語句檢查值,並應使用LINQ,WHERE語句來過濾列表,
我有類似
@using (Html.BeginForm())
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="Search" /></p>
}
視圖中的文本框設置然而,當我輸入客戶名稱,結果將不被過濾,我使用SQL Server 2008 R2中,有其他人遇到這個問題?還有其他什麼需要讓Contains方法工作嗎?
並建議感激?
利亞姆
感謝您的建議傢伙設法按照您的建議來整理它。 – Liam