1

我有一个搜索页面,有 4 个选项城市、州、年份、学校

单击提交后,您将该信息放在文本框中,它会查询数据库以查找匹配的记录。这行得通,但我想知道是否有更有效的做事方式,例如您可以按城市、州或学校和年份进行搜索,但每次我都必须这样做......

   if(string school == null && int year == null)
{
 // then search for city,state
}
if(string city == null && string state== null)
{
// then search for school, year
}
 else
{
  //  search for city,state,school,year
}

如您所见,这只有4 个字段,搜索每个字段变得乏味,特别是因为我计划在未来添加更多字段。有没有一种方法可以检查1 if 语句中的所有内容,例如,如果用户离开城市,则将其设为空以忽略它。

这是我的城市状态搜索示例

 public ActionResult search(string city, string state,string school,int year,int? page)
    {


        ViewBag.city = city;
        ViewBag.state = state;
        ViewBag.year = year;
        ViewBag.school = school;
        int pageSize = 10;
        int pageNumber = (page ?? 1);

        if (school == null && year ==null)
        {
  // I would like to put all my variables here and ignore blanks one like

            var article = (from x in db.relistings.OrderByDescending(x => x.relistingID)
                           where x.city == city && x.state == state select x);
            return View(article.ToPagedList(pageNumber, pageSize));
        }

}

4

0 回答 0