ASP.NET 4.5: Filtering using Model Binding in ASP.NET Web Forms
Posted by: Mahesh Sabnis ,
on 11/26/2011,
in
Category ASP.NET
Abstract: In this article, we will explore the ASP.NET 4,5 Model binding Value provider and use of Model binding for filtering master-detail or also known as parent-child data.
Note: This article is written using Visual Studio 2011 Preview and ASP.NET 4.5. Since these are preview versions, this article may change in the future.
In my previous article, we explored the new model binding mechanism in ASP.NET 4.5 and how easy it is to perform CRUD operations using it. While developing Data driven applications, a common requirement is the Master-Details filtering. Typically in earlier versions of ASP.NET, to achieve this we needed to write business logic for reading filtered value from the ‘Parent’ DataBound controls and then query the source data and bind the resultant with the ‘child’ Databound control. In this article, we will explore the ASP.NET 4.5 Model binding Value provider and use of Model binding for filtering master-detail or also known as parent-child data .
In ASP.NET 4.5, we are provided with the ‘System.Web.ModelBinding’ namespace. This namespace contains value provider classes e.g. ControlAttribute, QueryStringAttribute etc. These classed are inherited from ‘ValueProviderSourceAttribute’. This base class is used to define method parameters to specify source of values for model binding. The means that the parameter passed to the method specifies what value is to be taken for filtering records and what is the source of the value. This source may be Control, QueryString etc.
Let’s get started. Make sure you have read the previous article
In ASP.NET 4.5, we are provided with the ‘System.Web.ModelBinding’ namespace. This namespace contains value provider classes e.g. ControlAttribute, QueryStringAttribute etc. These classed are inherited from ‘ValueProviderSourceAttribute’. This base class is . The means that the parameter passed to the method specifies what value is to be taken for filtering records and what is the source of the value. This source may be Control, QueryString etc. Let’s get started. Make sure you have read the previous article
ASP.NET 4.5 - New Model Binding Feature Keeps the Web Form and Model Separate
Step 1: Open Visual Studio 2011 Developer Preview and create a ASP.NET Web Application targeted to .NET 4.5. Name it as ‘ASPNET45_ModelBinding’. To this project, add two new Folders with the name ‘Model’ and ‘Employee’.
Step 2: In the model folder, add a new ADO.NET Entity Framework and name it as ‘CompanyEDMX.edmx’. This EMD makes use of SQL Server 2008 and a Company Database in it.
The Schema of the Tables in the Company Database is as below:
Department - DeptNo (int) Primary Key, Dname (varchar(50)),Location (varchar(50)).
Employee - EmpNo (int) Primary Key, EmpName (varchar(50)),Salary (int), DeptNo(int) Foreign Key.
After the Wizard completes, the ADO.NET EF model will be as shown below:
Step 3: In the Employee folder, add a web form (with master page). Name it as ‘Employees.aspx’. Open the Employee.aspx in Source view and add DropDownList and GridView on it. Set properties of these controls as shown below:
The Entire Page Design markup will be as shown below:
Step 4: Open the Employees.aspx.cs and add the following code in it:
The important part in the code shown above is the ‘GetEmployees()’ method, which accepts a nullable parameter - DeptNo. This is defined using the [Control] attributes. This attribute class defines the constructor which accepts the ID of the control from which the source value is accepted. In the above case, the source control is the DropDownList with ID as ‘ddlDeptName’. Here the ‘GetEmployees()’ method accepts the DeptNo and based upon this value, the related Employees are read from the Employees collection.
Another important portion of the GridView code shown above is that the AllowPaging property is set to true. In the earlier versions of ASP.NET, it was necessary of a Developer to write the code for pagination in similar scenarioes. But in this case, the ‘GetEmployees()’ method returns ‘IQueryable’. Now when the end-user changes the page-index of the GridView, the query is automatically updated and the next page records are displayed.
Step 5: Run the Employees.aspx and select the Dname from the DropDownList. The following result will be displayed:
Conclusion: Using ASP.NET 4.5 Model Binding Value providers, communication between controls can be made possible by using less code.
The entire source code of this article can be downloaded over here
Was this article worth reading? Share it with fellow developers too. Thanks!