使用DropDowns和ViewData进行过滤

时间:2013-03-25 14:45:50

标签: c# visual-studio-2010 asp.net-mvc-4 drop-down-menu filter

我一直在论坛上寻找,并试图通过下拉菜单过滤我的索引页面。

这是我想出的。

首先,我的控制器看起来像这样:

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

终于成功了!!!

现在,如果我想在我的样式下拉列表中删除重复项,那该怎么办?

2 个答案:

答案 0 :(得分:0)

您不应对DropDownList第一个参数和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);
}

答案 1 :(得分:0)

让我们从显而易见的事情开始,你需要更好地命名你的对象,如果你已经很快就看到了这个明显的问题,看看:

    /*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-3fb9811b http://blogs.msdn.com/b/charlie/archive/2006/11/19/linq-farm-group-and-distinct.aspx