我一直在论坛上四处寻找,因此试图使用下拉列表过滤我的索引页面。
这是我想出的。
首先我的控制器看起来像这样:
public ActionResult Index(string searchString)
{
var projects = from s in db.Project select s;
var themeList = db.Theme.ToList();
var projectList = db.Project.ToList();
if (Request.Form["FilterTheme"] != null && Request.Form["FilterTheme"] != "")
{
int i = int.Parse(Request.Form["FilterTheme"]);
projects = from s in db.Project
from c in s.Themes
where c.ThemeID == i
select s;
}
if (Request.Form["Styles"] != null && Request.Form["Styles"] != "")
{
int i = int.Parse(Request.Form["Styles"]);
projets = from s in db.Project
where s.ID == i
select s;
}
ViewData["Theme"] = new SelectList(themeList,"ThemeID", "Name");
ViewData["Style"] = new SelectList(projectList, "ID", "Style");
return View(projects);
}
视图看起来像:
@using (Html.BeginForm())
{
<table>
<thead>
<tr align="left">
<th>
Project name :
</th>
<th>
Theme(s) :
<br /><br />
@Html.DropDownList("FilterTheme", (SelectList)ViewData["Theme"], "Select a theme", new { onchange = "this.form.submit()" })
</th>
<th>
Style :
<br /><br />
@Html.DropDownList("Styles", (SelectList)ViewData["Style"], "Select a style", new { onchange = "this.form.submit()" })
</th>
<th>
Date :
</th>
</tr>
</thead>
<tbody>
...
</tbody>
</table>
}
终于成功了!!!
现在,如果我想删除样式下拉列表中的重复项,那将如何工作?