title: EF + MVC3: How to Create DropDownList Filter when page model is IPagedList?
body: <p>I've been following the excellent tutorial from asp.net about creating pagination and filtering: <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application" rel="nofollow">http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application</a></p>
  
  <p>I now want to add another filter using a dropdownlist which will filter my projects. I found another tutorial about this and followed it: <a href="http://chikkanti.wordpress.com/2012/04/30/dropdownlist-based-filtering-in-mvc-3-using-entityframeworkedm-modalfirst-method/" rel="nofollow">http://chikkanti.wordpress.com/2012/04/30/dropdownlist-based-filtering-in-mvc-3-using-entityframeworkedm-modalfirst-method/</a></p>
  
  <p>However, when I run it, I got an error about the dropdownlist not being of type IEnumerable. This made sense as the page model is of type IPagedList.
  How can I add a dropdownlist filter to the page with page model IPagedList?</p>
  
  <p>Controller:</p>
  
  <pre><code>        public ActionResult SearchNames(string ddlcontent, int? page)
      {
          int pageSize = 10;
          int pageNumber = (page ?? 1);
          var list = new List&lt;string&gt;();
          var nameqry = from n in db.tblProjects
                        select n.ProjectName;
          list.AddRange(nameqry.Distinct());
          ViewBag.ddlcontent = new SelectList(list);
          //var names = from m in db.tblProjects
          //            select m;
          var tbldefectdetails = db.tblDefectDetails.Include(t =&gt; t.tblBugLocation).Include(t =&gt; t.tblBugType).Include(t =&gt; t.tblBusinessUnit).Include(t =&gt; t.tblDefectDiscoveryMethod).Include(t =&gt; t.tblProject).Include(t =&gt; t.tblLanguage);
          if (!string.IsNullOrEmpty(ddlcontent))
              {
                  tbldefectdetails = tbldefectdetails.Where(s =&gt; s.tblProject.ProjectName.ToUpper().Contains(ddlcontent.ToUpper()));
              }
          return View(tbldefectdetails.ToPagedList(pageNumber, pageSize));
      }
  </code></pre>
  
  <p>DropdownList in View:</p>
  
  <pre><code>@model PagedList.IPagedList&lt;EDTToolMVC.Models.tblDefectDetail&gt;
  
  @{
      ViewBag.Title = "Early Defect Tracking Tool - Home";
  }
  
  &lt;h2&gt;SearchNames&lt;/h2&gt;
  @using (@Html.BeginForm(“SearchNames”, “Names”, FormMethod.Get))
  {
  @Html.DropDownList(“ddlcontent”, “All”)&lt;input type=”submit” value=”Filter” /&gt;;
  }
  </code></pre>
  
  <p>IPagedList:</p>
  
  <pre><code>namespace PagedList
  {
  // Summary:
  //     Represents a subset of a collection of objects that can be individually accessed
  //     by index and containing metadata about the superset collection of objects
  //     this subset was created from.
  //
  // Type parameters:
  //   T:
  //     The type of object the collection should contain.
  //
  // Remarks:
  //     Represents a subset of a collection of objects that can be individually accessed
  //     by index and containing metadata about the superset collection of objects
  //     this subset was created from.
  public interface IPagedList&lt;out T&gt; : IPagedList, IEnumerable&lt;T&gt;, IEnumerable
  {
      // Summary:
      //     Gets the number of elements contained on this page.
      int Count { get; }
  
      // Summary:
      //     Gets the element at the specified index.
      //
      // Parameters:
      //   index:
      //     The zero-based index of the element to get.
      T this[int index] { get; }
  
      // Summary:
      //     Gets a non-enumerable copy of this paged list.
      //
      // Returns:
      //     A non-enumerable copy of this paged list.
      IPagedList GetMetaData();
  }
  }
  </code></pre>