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に同じ名前を使用しないでください。あなたの場合、あなたはStyle2回使用しましたが、これは間違っています。別の名前を使用する必要があります。

@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 = 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

于 2013-03-26T14:59:04.473 に答える