Filter Data Using Model Binding in ASP.NET 4.5

Introduction

In this article we will learn how to filter data using Model Binding. This article will also help you to optionally filter the employee results depending on whether a product name includes a keyword.

Earlier versions of ASP.Net have the ObjectDataSource Control but ASP.NET 4.5 has integrated it with Data Controls. New properties are included in ASP.NET 4.5 such as SelectMethod, UpdateMethod and DeleteMethod.

If the developer wants to bind data controls then he can use SelectMethod and also if he wants to do other data control operations then he can do them using UpdateMethod and DeleteMethod.

Step 1

First of all create a new Web Application in V.S 2012.

pic1.jpg

Step 2

Now add the Grid View to your Web Page.

pic2.jpg

Step 3

In the source code of the Grid View add the SelectMethod, as in:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    
<title></title>
</
head>
<
body>
    
<form id="form1" runat="server">
    
<div>
           
<asp:GridView ID="GridView2" runat="server" SelectMethod="GetEmployee" AutoGenerateColumns="true" IemType="">  
        
</asp:GridView>
     
</div>
    
</form>
</
body>
</html>

Step 4

Now we will modify our code by creating a public method in theWebForm1.aspx.cs page that will return a list of employees; see:

public class Employee
        {
            
public int ID { get; set; }
            
public string Name { get; set; }
            
public int Salary { get; set; }
            
public string Designation { get; set; }
        }
 
public IEnumerable<Employee> GetEmployee([QueryString("q")]string keyword)
        {
                            IList<Employee> emplist = new List<Employee>();
                emplist.Add(
new Employee { ID = 1, Name = "Jones", Salary = 4000,Designation="Trainee" });
                emplist.Add(
new Employee { ID = 2, Name = "Jimmy", Salary = 4000,Designation="Trainee" });
                emplist.Add(
new Employee { ID = 3, Name = "Jacob", Salary = 12000,Designation="Developer" });
                emplist.Add(
new Employee { ID = 4, Name = "Janet", Salary = 25000,Designation="HR" });
                emplist.Add(new Employee { ID = 5, Name = "Jamal", Salary = 4000,Designation="Trainee" });
        }

Step 5

Now to filter the Employee we will use a keyword depending on the data to be selected, but remember that the data will be filtered only if the given keyword matches any of the data in the table. See:

public IEnumerable<Employee> GetEmployee([QueryString("q")]string keyword)
        {
            
IEnumerable<Employee> query = null;
            
if (keyword != null)
            {
                
IList<Employee> emplist = new List<Employee>();
                emplist.Add(
new Employee { ID = 1, Name = "Jones", Salary = 4000,Designation="Trainee" });
                emplist.Add(
new Employee { ID = 2, Name = "Jimmy", Salary = 4000,Designation="Trainee" });
                emplist.Add(
new Employee { ID = 3, Name = "Jacob", Salary = 12000,Designation="Developer" });
                emplist.Add(
new Employee { ID = 4, Name = "Janet", Salary = 25000,Designation="HR" });
emplist.Add(new Employee { ID = 5, Name = "Jamal", Salary = 4000,Designation="Trainee" });
                query = emplist.Where(p => p.Designation.Contains(keyword));

            }                

       return query;

Step 6

Now debug your program.

You will first get a clear screen in the browser but to see any of the results you must pass the value to the Keyword. That will be done by using "?q=Trainee". Here Trainee is only a value that is passed to the keyword, you can pass whatever value you need to.

pic3.jpg


Similar Articles