MVC ASP.NET搜索过滤器由int

时间:2014-02-28 21:17:36

标签: asp.net-mvc asp.net-mvc-5

是否可以通过搜索int来过滤视图记录?它只是一个字符串,但是当我尝试任何调整它来绘制所有匹配int的记录时我得到错误:DbComparisonExpression需要具有可比类型的参数。参考返回视图。

控制器:

 public ActionResult Index(string sortOrder,string search)
      {
        ViewBag.TimeSortParam = String.IsNullOrEmpty(sortOrder) ? "Time_desc" : "";
        ViewBag.PrioritySortParam = sortOrder == "Priority" ? "Priority_desc" : "";
        var incidents = from s in db.Incidents.Include(i => i.Defect).Include(i =>   i.Operator)
                        select s;

        if (!String.IsNullOrEmpty(search))
        {
            incidents = incidents.Where(s => s.IncidentID.Equals(search));
        }
 return View(incidents.ToList());

查看

Find by id: @Html.TextBox("Search")

我可以看到问题是我正在使用字符串,但我无法找到int

的解决方案

2 个答案:

答案 0 :(得分:0)

在您的陈述中,请更改为:

s.IncidentID==Convert.ToInt32(search)

答案 1 :(得分:0)

如果有人对此感兴趣,虽然我不确定它是通过int过滤的最有效方式:

public ActionResult Index(string sortOrder, int? search)
    {
        ViewBag.TimeSortParam = String.IsNullOrEmpty(sortOrder) ? "Time_desc" : "";
        ViewBag.PrioritySortParam = sortOrder == "Priority" ? "Priority_desc" : "";
        var incidents = from s in db.Incidents.Include(i => i.Defect).Include(i => i.Operator)
                        select s;
        if (search != null)
        {
            incidents = incidents.Where(s => s.IncidentID == (search));
        }}
 return View(incidents.ToList());