title: How to filter data using dropdown list in mvc
body: <p>My site already have the search,sorting function. I wanted to add a filter function also but am facing a problem which is  the dropdown list at the view page will not return a value to the controller that will filter out the data but somehow it won't and I didn't want to hard code the dropdown list if that is possible . <br>
  Admin Controller</p>
  <pre><code>public ViewResult Index(string sortOrder, string currentFilter, string searchString, string currentValue, string sortType, int? page)
          {
              ViewBag.CurrentSort = sortOrder;
              ViewBag.sortType = new SelectList(db.MembershipTypes, &quot;MembershipTypesId&quot;, &quot;Name&quot;);
              ViewBag.CurrentType = sortType;
  
              //sorting
              ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? &quot;name_desc&quot; : &quot;&quot;; 
              ViewBag.DateSortParm = sortOrder == &quot;Date&quot; ? &quot;date_desc&quot; : &quot;Date&quot;;
  
              if (searchString != null || sortType != null)
              {page = 1;}
              else
              {
                  searchString = currentFilter;
                  sortType = currentFilter;
              }
              ViewBag.CurrentFilter = searchString;
  
  
              var customer = from a in db.Customers.Include(c =&gt; c.MembershipTypes) select a;
              var x = from b in db.MembershipTypes select b;
  
              //search
              if (!String.IsNullOrEmpty(searchString))
              {
                  customer = customer.Where(c =&gt; c.LastName.Contains(searchString)
                                         || c.FirstName.Contains(searchString));
              }
  
              //sorting
              switch (sortOrder)
              {
                  case &quot;name_desc&quot;:
                      customer = customer.OrderByDescending(c =&gt; c.LastName);
                      break;
                  case &quot;Date&quot;:
                      customer = customer.OrderBy(c =&gt; c.DateofBirth);
                      break;
                  case &quot;date_desc&quot;:
                      customer = customer.OrderByDescending(c =&gt; c.DateofBirth);
                      break;
                  default:
                      customer = customer.OrderBy(c =&gt; c.LastName);
                      break;
              }
  
              int pageSize = 3;
              int pageNumber = (page ?? 1);
              return View(customer.ToPagedList(pageNumber, pageSize));
  
          }
  
  
  </code></pre>
  <p>Index.cshtml</p>
  <pre><code>@model PagedList.IPagedList&lt;LeafLife.Models.Customers&gt;
  @using PagedList.Mvc;
  
  &lt;link href=&quot;~/Content/PagedList.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
  
  @{
      ViewBag.Title = &quot;Index&quot;;
      Layout = &quot;~/Views/Shared/_Layout.cshtml&quot;;
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  @using (Html.BeginForm(&quot;Index&quot;, &quot;Admin&quot;, FormMethod.Get))
  {
  &lt;p&gt;
      Find by name: @Html.TextBox(&quot;SearchString&quot;, ViewBag.CurrentFilter as string)
      @Html.DropDownList(&quot;MembershipTypesId&quot;, ViewBag.CurrentFilter as string)
  
      &lt;input type=&quot;submit&quot; value=&quot;Search&quot; /&gt;
  &lt;/p&gt;
  }
  
  &lt;table class=&quot;table&quot;&gt;
      &lt;tr&gt;
          &lt;th&gt;
              MembershipTypes
          &lt;/th&gt;
          &lt;th&gt;
              Username
          &lt;/th&gt;
          &lt;th&gt;
              FirstName
          &lt;/th&gt;
          &lt;th&gt;
              @Html.ActionLink(&quot;Last Name&quot;, &quot;Index&quot;, new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
          &lt;/th&gt;
          &lt;th&gt;
              Email
          &lt;/th&gt;
          &lt;th&gt;
              @Html.ActionLink(&quot;Date of Birth&quot;, &quot;Index&quot;, new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
          &lt;/th&gt;
          &lt;th&gt;
              Password
          &lt;/th&gt;
  
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
      @foreach (var item in Model)
      {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.MembershipTypes.Name)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Username)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.FirstName)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.LastName)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Email)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.DateofBirth)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Password)
              &lt;/td&gt;
  
              &lt;td&gt;
                  @Html.ActionLink(&quot;Edit&quot;, &quot;Edit&quot;, new { id = item.IC }) |
                  @Html.ActionLink(&quot;Details&quot;, &quot;Details&quot;, new { id = item.IC }) |
                  @Html.ActionLink(&quot;Delete&quot;, &quot;Delete&quot;, new { id = item.IC })
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  
  &lt;/table&gt;
  
  &lt;br /&gt;
  Page @(Model.PageCount &lt; Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
  
  @Html.PagedListPager(Model, page =&gt; Url.Action(&quot;Index&quot;,
      new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
  
  </code></pre>