MVC具有搜索功能的条件

时间:2012-04-13 12:15:22

标签: asp.net-mvc-3 where

我正在尝试使用actionLink创建搜索功能。

actionLink会为控制器提供参数,例如

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

我传递了这个值,它在控制器中工作。但是,当我尝试比较十进制数据类型时,不是字符串数据类型。目前,处理器是varchar,dissplaySize是十进制。

我不知道如何使用searchString处理displaySize。

我的控制器就是这个。

//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 个答案:

答案 0 :(得分:1)

肯定a.displaySize.ToString().Contains(searchString.ToUpper()));无法达到目的。尝试将十进制值的整数部分与搜索字符串进行比较,以便提供更准确的结果。像这样Convert.ToInt32(a.displaySize).ToString().Contains(searchString)