0

我一直在论坛上四处寻找,因此试图使用下拉列表过滤我的索引页面。

这是我想出的。

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

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

终于成功了!!!

现在,如果我想删除样式下拉列表中的重复项,那将如何工作?

4

2 回答 2

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);
}
于 2013-03-26T14:55:56.890 回答
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 = 样式,但您正在根据项目 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

于 2013-03-26T14:59:04.473 回答