Filter Data by Values in Model Binding Using Drop Down List in .NET 4.5

Introduction

 
In this article, we will learn how to filter data by values in Model Binding using a Drop Down List.
 
In my previous article, I told you about filtering data using Model Binding but in that article, we were not using any type of drop-down list, we had to filter data by making changes in the URL section.
 
Step 1
Add the Drop Down List to your Web Page and add the following properties in its Source Code.
  1. <asp:DropDownList ID="employee" runat="server" SelectMethod="GetEmployee" DataTextField="Name" AutoPostBack="true" AppendDataBoundItems="true" DataValueField="ID">  
  2.     <asp:ListItem Text="all" Value="" />  
  3. </asp:DropDownList> 
Step 2
I am also adding the EmptyDataTemplate element to the Grid View so that if no product matches the data the user requested then it can provide a message.
  1. <asp:GridView ID="GridView1" runat="server" SelectMethod="GetEmployee" AutoGenerateColumns="true">  
  2.     <EmptyDataTemplate>No Item match to your selection </EmptyDataTemplate>  
  3. </asp:GridView> 
Step 3
Now we have to update the GetEmployee method to take a new parameter that contains the ID of the selected Name from the Drop Down List.
  1. public IEnumerable < Employee > GetEmployee([QueryString("q")] string keyword, [Control("Employee")] int ? ID) {  
  2.     IEnumerable < Employee > query = null;  
  3.     if (keyword != null) {  
  4.         IList < Employee > emplist = new List < Employee > ();  
  5.         emplist.Add(new Employee { ID = 1, Name = "Jones", Salary = 4000, Designation = "Trainee" });  
  6.         emplist.Add(new Employee { ID = 2, Name = "Jimmy", Salary = 4000, Designation = "Trainee" });  
  7.         emplist.Add(new Employee { ID = 3, Name = "Jacob", Salary = 12000, Designation = "Developer" });  
  8.         emplist.Add(new Employee { ID = 4, Name = "Janet", Salary = 25000, Designation = "HR" });  
  9.         emplist.Add(new Employee { ID = 5, Name = "Jamal", Salary = 4000, Designation = "Trainee" });  
  10.         query = emplist.Where(p => p.Designation.Contains(keyword));  
  11.     }  
  12.     if (ID.HasValue && ID.Value > 0) {  
  13.         query = query.Where(p => p.ID == ID);  
  14.     }  
  15.     return query;  
Step 4
The complete code is as follows:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Web.ModelBinding;  
  8.   
  9. namespace WebApplication5 {  
  10.     public partial class WebForm1: System.Web.UI.Page {  
  11.         public class Employee {  
  12.             public int ID { get;  
  13.                 set; }  
  14.             public string Name { get;  
  15.                 set; }  
  16.             public int Salary { get;  
  17.                 set; }  
  18.             public string Designation { get;  
  19.                 set; }  
  20.         }  
  21.   
  22.         public IEnumerable < Employee > GetEmployee([QueryString("q")] string keyword, [Control("Employee")] int ? ID) {  
  23.             IEnumerable < Employee > query = null;  
  24.             if (keyword != null) {  
  25.                 IList < Employee > emplist = new List < Employee > ();  
  26.                 emplist.Add(new Employee { ID = 1, Name = "Jones", Salary = 4000, Designation = "Trainee" });  
  27.                 emplist.Add(new Employee { ID = 2, Name = "Jimmy", Salary = 4000, Designation = "Trainee" });  
  28.                 emplist.Add(new Employee { ID = 3, Name = "Jacob", Salary = 12000, Designation = "Developer" });  
  29.                 emplist.Add(new Employee { ID = 4, Name = "Janet", Salary = 25000, Designation = "HR" });  
  30.                 emplist.Add(new Employee { ID = 5, Name = "Jamal", Salary = 4000, Designation = "Trainee" });  
  31.                 query = emplist.Where(p => p.Designation.Contains(keyword));  
  32.             }  
  33.             if (ID.HasValue && ID.Value > 0) {  
  34.                 query = query.Where(p => p.ID == ID);  
  35.             }  
  36.             return query;  
  37.         }  
  38.     }  
Step 5
There is one more important thing to remember; that is to add the QueryString("q"). You first had to add the Namespace "System.Web.ModelBinding;" otherwise it will give an error in your code.
 
Step 6
Now on debugging the code you can select the Name from the Drop Down List and the Grid View will show the filtered data because it will be automatically rebounded. This is possible because Model Binding tracks the value of parameters for the SelectMethod and determine whether any parameter value has changed after a post-back. If so, Model Binding will force the associated data control to rebind the data.
 
model1.jpg
 
In the picture above all the data has been shown, but now I will select one of the Names from the Drop Down List.
 
model2.jpg


Similar Articles