过滤与下拉菜单和ViewData

本文关键字:ViewData 下拉菜单 过滤 | 更新日期: 2023-09-27 18:05:37

我一直在论坛上寻找,所以试图用下拉菜单过滤我的索引页。

这是我想到的。

首先我的控制器是这样的:

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>
}

终于成功了!!

现在如果我想在样式下拉菜单中删除重复项,那该怎么做呢?

过滤与下拉菜单和ViewData

让我们从最明显的事情开始,你需要更好地命名你的对象,如果你有你会很快发现这个明显的问题,看一下:

    /*if (Request.Form["Style"] != null && Request.Form["Style"] != "")
    {
        int i = int.Parse(Request.Form["Style"]);
        projets = from s in db.Project
                  from c in s.Style
                  where s.ID == i
                  select s;
    }*/

您正在设置i = style,但是您正在根据项目id检查它,这里的重命名是您当前拥有的:

    if (Request.Form["Style"] != null && Request.Form["Style"] != "")
    {
        int styleID = int.Parse(Request.Form["Style"]);
        projects = from projects in db.Project
                  from styles in projects.Style
                  where projects.ID == styleID
                  select projects;
    }

我猜这是你真正想要的:

    if (Request.Form["Style"] != null && Request.Form["Style"] != "")
    {
        int styleID = int.Parse(Request.Form["Style"]);
        projects = (from projects in db.Project
                  where projects.StyleID == styleID
                  select projects).Distinct();
    }

根据要求,我在那里添加了. distinct()来删除重复项。以下是一些资源:

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811bhttp://blogs.msdn.com/b/charlie/archive/2006/11/19/linq-farm-group-and-distinct.aspx

您不应该为DropDownList first参数和ViewData使用相同的名称。在你的例子中,你用了两次Style,这是错误的。您应该使用单独的名称:

@Html.DropDownList(
    "SelectedStyle", 
    (SelectList)ViewData["Styles"], 
    "Select a style", 
    new { onchange = "this.form.submit()" }
)

和你的控制器:

public ActionResult Index(int? filterTheme, int? selectedStyle)
{
    var projects = from s in db.Project select s;
    if (filterTheme != null)
    {
        projects = from s in db.Project
                   from c in s.Themes
                   where c.ThemeID == filterTheme.Value
                   select s;             
    }
    if (selectedStyle != null)
    {
        projects = from s in projects
                   from c in s.Style
                   where s.ID == selectedStyle.Value
                   select s;
    }
    ViewData["Theme"] = new SelectList(db.Theme.ToList(), "ThemeID", "Name");
    ViewData["Styles"] = new SelectList(db.Project.ToList(), "ID", "Style");
    return View(projects);
}