Asp.net mvc 3 mvc3搜索页面条件
我有一个搜索页面,有4个选项:城市、州、年份、学校 单击“提交”后,您会将该信息放入文本框,它会查询数据库以查找匹配的记录。这是可行的,但我想知道是否有更有效的方法做事情,例如,你可以搜索城市,州或学校和年份,但每次我必须这样做Asp.net mvc 3 mvc3搜索页面条件,asp.net-mvc-3,if-statement,Asp.net Mvc 3,If Statement,我有一个搜索页面,有4个选项:城市、州、年份、学校 单击“提交”后,您会将该信息放入文本框,它会查询数据库以查找匹配的记录。这是可行的,但我想知道是否有更有效的方法做事情,例如,你可以搜索城市,州或学校和年份,但每次我必须这样做 if(string school == null && int year == null) { // then search for city,state } if(string city == null && string st
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));
}
}