使用DropDown和ViewData进行筛选

问题描述:

我一直在四处搜索论坛,所以试图让我的索引页面使用下拉菜单进行筛选。使用DropDown和ViewData进行筛选

这是我想出来的。

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

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

终于成功了!

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

+0

有你检出或看着什么是关于主题VS样式下拉不同。一切看起来都是一样的我唯一能想到的就是检查以确保'ViewData [“Style”]'有数据并且不是'null',这是我想到的第一件事情.. – MethodMan 2013-03-25 14:50:22

+0

我检查过之前,ViewData正确返回选定的值。然而,它不会过滤我想要的方式。很确定我错过了一些专业:/ – 2013-03-25 14:55:44

那么让我们先从明显的事情,你需要命名的对象比较好,如果你有,你会看到这个明显的很快的问题,看看:

/*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

+0

嗨迈克,在这里你不能去:哪里styles.ID == styleID,因为我不能在这种情况下采取styles.ID。奇怪的。 – 2013-03-26 15:14:39

+0

我认为Id是你的风格表上的一列,是不是这种情况?无论哪种方式,你似乎不太可能想要project.Id == styleID。 – 2013-03-26 15:25:50

+0

样式不是表格,它是项目表格中的一个值。我想要做的是按所选样式过滤项目列表。 – 2013-03-26 15:28:00

不应该为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); 
} 
+0

的确如此,这是我的问题中的一个错字。我一直在做着与你所提到的完全相同的方式,但它不会按照我希望的方式进行过滤。当我选择一种风格时,它总是给我返回所有项目的列表。 – 2013-03-26 14:58:49

+0

你的过滤器可能是错误的。我已经用示例控制器操作更新了我的答案。 – 2013-03-26 15:01:32

+0

我试着用你刚才提到的,它看起来可能是答案,但它不是我想要做的。我用你的主题过滤器来查看它是否可以工作,没有任何反应。它总是给我返回所有项目的列表。我的方法已经适用于主题,所以我会保持这种方式。 – 2013-03-26 15:11:30