MVC凡与搜索功能的条件 [英] MVC Where condition with search function
问题描述
我试图与ActionLink的搜索功能。
在ActionLink的会给参数控制器,如
@ Html.ActionLink(英特尔酷睿i5,搜索,店,新{@searchString =I5},NULL)
我通过该值,它是工作在控制器。然而,当我尝试比较十进制数据类型,不串数据类型。目前,处理器为varchar和dissplaySize是小数。
我不知道如何与搜索字符串处理显示尺寸。
我的控制器是这样的。
//这里Controller
公众的ActionResult搜索(字符串搜索字符串)
{ 变种产品=从在_db.Product.Include(A => a.Category)
选择一个;
如果(!String.IsNullOrEmpty(搜索字符串))
{
//处理器工作,因为搜索字符串是字符串,但是,
//显示尺寸是行不通的,因为十进制值,
//我不知道怎么写Where条件在这里。
产物= product.Where(一个=方式> a.processor.ToUpper()包含(searchString.ToUpper())
||
//a.displaySize.ToString()包含(searchString.ToUpper()));
//我repalce低于code,但它的主要错误
//'实体不识别方法'System.String的ToString()方法。
Convert.ToDecimal(a.displaySize)的ToString()。包含(搜索字符串) }
返回查看(product.ToList());
}
绝对 a.displaySize.ToString()包含(searchString.ToUpper()));
难道不服务puprpose。尝试用比较搜索字符串,将给予更精确的结果十进制值的整数部分。像这样的 Convert.ToInt32(a.displaySize)的ToString()。包含(搜索字符串)
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());
}
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)
这篇关于MVC凡与搜索功能的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!