MVC Where condition with search function

2019-08-26 11:30发布

问题:

I am trying to make search function with actionLink.

the actionLink will give parameter to controller such as

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

I pass this value and it is working in controller. However, when I trying to compare decimal datatype, not string datatype. Currently, processor is varchar, and dissplaySize is decimal.

I don't know how to handle displaySize with searchString.

My controller is this.

//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:

definitely a.displaySize.ToString().Contains(searchString.ToUpper())); wouldnt serve the puprpose. try comparing the integer part of the decimal value with the search string that will give more accurate results. like this Convert.ToInt32(a.displaySize).ToString().Contains(searchString)