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.

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:

Department Employee Table

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.

Conclusion: Using ASP.NET 4.5 Model Binding Value providers, communication between controls can be made possible by using less code.