2011-07-13 74 views
0

我想要的客戶名單上實現搜索功能,該功能將在本教程的Asp.Net網站的詳細,Asp.Net MVC3添加搜索功能

http://www.asp.net/entity-framework/tutorials/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

在我的控制我有以下

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") &nbsp; 
    <input type="submit" value="Search" /></p> 
} 

視圖中的文本框設置然而,當我輸入客戶名稱,結果將不被過濾,我使用SQL Server 2008 R2中,有其他人遇到這個問題?還有其他什麼需要讓Contains方法工作嗎?

並建議感激?

利亞姆

回答

2

在排序順序開關你重新設置與repository.Customers的卡斯特變量。您應該使用cust變量來對已過濾的集合應用排序。

switch (sortOrder) 
     { 
      case "CustomerName desc": 
       cust = cust.OrderByDescending(s => s.CustomerName); 
       break; 
+0

感謝您的建議傢伙設法按照您的建議來整理它。 – Liam

0

當你搜索你的if語句,但它仍然去到switch語句後,嘗試

if (!String.IsNullOrEmpty(searchString)) 
    { 
     cust = cust.Where(c => c.CustomerName.ToUpper().Contains(searchString.ToUpper()) 
           || c.PrimaryContactName.ToUpper().Contains(searchString.ToUpper())); 
    } 
    else 
    { 
     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; 
     } 
    } 

這樣卡斯特心不是被它集之後再分配

編輯:既然你在你的方法的開始設置cust,你也可以有

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 = cust.OrderByDescending(s => s.CustomerName); 
      break; 
     case "PrimaryContactName": 
      cust = cust.OrderBy(s => s.PrimaryContactName); 
      break; 
     case "PrimaryContactName desc": 
      cust = cust.OrderByDescending(s => s.PrimaryContactName); 
      break; 
     default: 
      cust = cust.OrderBy(s => s.CustomerName); 
      break; 
    } 

仍下令卡斯特無需重新加載它