2012-04-13 76 views
0

我正在嘗試使用actionLink進行搜索功能。MVC條件與搜索功能

的ActionLink的會給參數到控制器如

@Html.ActionLink("Intel Core i5", "Search", "Store", new { @searchString = "i5" }, null) 

我通過此值,並將其工作在控制器。但是,當我試圖比較小數數據類型,而不是字符串數據類型。目前,處理器是varchar,而dissplaySize是十進制的。

我不知道如何處理displaySize與searchString。

我的控制器是這樣的。

//Controller here 
public ActionResult Search(string searchString) 
    { 

     var product = from a in _db.Product.Include(a => a.Category) 
         select a; 
     if (!String.IsNullOrEmpty(searchString)) 
     { 
      //processor is working because searchString is string, however, 
      //displaySize is not working because of decimal value, and 
      // I don't know how to write Where condition in here. 
      product = product.Where(a => a.processor.ToUpper().Contains(searchString.ToUpper()) 
           || 
        //a.displaySize.ToString().Contains(searchString.ToUpper())); 
      //I repalce to below code, but it has error 
      //'Entity does not recognize the method ''System.String ToString()' method. 
        Convert.ToDecimal(a.displaySize).ToString().Contains(searchString) 

     } 
     return View(product.ToList()); 
    } 

回答

1

絕對a.displaySize.ToString().Contains(searchString.ToUpper())); wouldnt派上用場。嘗試將十進制值的整數部分與搜索字符串進行比較,以得到更準確的結果。像這樣Convert.ToInt32(a.displaySize).ToString().Contains(searchString)

+0

比你回覆,但我有錯誤,'LINQ to Entities無法識別方法System.String ToString()'方法'。如何將十進制轉換爲字符串與searchString進行比較? – wholee1 2012-04-13 14:19:15

+0

那麼你在你提供的例子中如何做到這一點?這是你實際發佈這個問題的錯誤嗎? – 2012-04-13 14:22:44

+0

是的。只是我把我之前發佈的代碼取代了我之前發佈的代碼。我把這個'Convert.ToInt32(a.displaySize).ToString()。包含(searchString)'放在Where條件中。 – wholee1 2012-04-13 14:25:15