您好,我正在尝试通过下拉过滤器在索引页面中按毕业状态进行过滤,但是单击“处理”按钮时,页面将不会过滤...
我在这里做什么错了?
Index Page showing filter button
查看:
@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());
}
答案 0 :(得分:0)
因为您不需要根据初始页面加载进行过滤。初始加载时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());
}