您好,我正在尝试使用下拉式筛选器按索引页面中的毕业状态进行筛选,但是单击“处理”按钮时,页面将无法筛选...
我在这里做错了什么?
查看:
@using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { @class =
"FilterForm" }))
{
<table>
<tr>
<th>
@Html.DropDownList("GraduationStatus",
null, htmlAttributes: new { @class = "form-control" })
</th>
<th>
<button type="submit" class="btn btn-
primary btn-lg"> Process !</button>
</th>
</tr>
</table>
}
控制器:
public ActionResult Index(string graduationStatus)
{
ViewBag.GraduationStatus = new SelectList(db.Graduated_Students.Select(m => m.GraduationStatus).Distinct().ToList());
var graduates = db.Graduated_Students.Where(student => student.GraduationStatus != null);
return View(graduates.ToList());
}
因为您不需要根据初始页面加载进行过滤。初始加载时gradientStatus将为null,因此添加一个条件以检查gradientStatus是否具有某些值。像这样
public ActionResult Index(string graduationStatus)
{
var graduates = db.Graduated_Students.Where(student => student.GraduationStatus != null);
ViewBag.GraduationStatus = new SelectList(db.Graduated_Students.Select(m => m.GraduationStatus).Distinct().ToList());
if(!string.IsNullOrEmpty(graduationStatus))
{
graduates = graduates .Where(student => student.GraduationStatus == graduationStatus);
}
return View(graduates.ToList());
}
究竟!完美的工作,谢谢!