Model Binding Feature in Asp.Net 4.5
Model
binding isn’t exactly a “new” feature when it comes to .NET because ASP.NET MVC
has had it for a long time. However, it’s new to ASP.NET Web Forms and yet another
feature that will truly change how you write your application code if you take
advantage of what it offers.
Model
binding is the process of getting model objects in and out of controls without
writing a lot of plumbing code to do it. You can now bind data controls
directly to methods that provide select, insert, update and delete
functionality. When the methods are called you don’t have to write a lot of
code to access the values that were posted back in the case of update, insert,
or delete operations. Instead, model binding allows you to have a given model
object’s properties (a class with properties if you’re not familiar with model
objects) automatically filled with the posted back data.
Data
bound controls are the major part of typical web form applications and we spend
a lot of time in writing code related to different events of that control.
The
new functionality makes task easier so that you as a developer can focus on
data and entities. To better understand the code let me first explain the
scenario.
We want to display all the records of above table in GridView control which also supports Paging and Sorting functionality. How would we do this?
OLDER WAY
To
display data in Gridview, we required markup like that:
<asp:GridView ID="gvCustomer"
runat="server" AllowSorting="true"
OnSorting="gvCustomer_Sorting">
</asp:GridView>
To
populate GridView from code-behind, we required code as:
var dataContext = new
DatabaseDataContext(Classes.GetConnectionString);
var CustomerList = (from e in dataContext.Customers
select e).ToList();
if (CustomerList.Count > 0)
{
gvCustomer.DataSource
= CustomerList;
gvCustomer.DataBind();
}
And
we need to create separate events of GridView, in order to use Paging or
Sotring like,
protected void gvCustomer_Sorting(object sender,
GridViewSortEventArgs e)
{
var
dataContext = new DatabaseDataContext(Classes.GetConnectionString);
var CustomerList
= (from e in dataContext.Customers
select e).ToList();
if (CustomerList.Count
> 0)
{
gvCustomer.DataSource
= CustomerList;
gvCustomer.DataBind();
}
}
NEW WAY
Asp.Net
4.5 introduce a new feature called Model Binding which take advantage of
System.Linq.IQueryable type.
public IQueryable<Customer> GetCustomersList()
{
var
dataContext = new DatabaseDataContext(Classes.GetConnectionString);
IQueryable<Customers> CustomerList = (from e in dataContext.Customers
select e).ToList();
return
CustomerList;
}
Now
write a Gridview control markup as:
<asp:GridView ID="gvCustomer"
runat="server"
ItemType="DataBindingSample.Customers" SelectMethod="GetCustomers">
</asp;GridView>
Here:
·
ItemType
refers to table name define within IQueryable tag
·
SelectMethod
refers to name of method
Now
run the application, it will show you all records from table "Customer"
in GridView. If you want to add Paging or Sorting functionality, go ahead just
allow both functionality in GridView markup and rest of things will be handle
by Model Binding itself.
It
would change the way how data controls were generally managed in web form
applications.
Model
binding allows model objects to be bound directly to controls and allows data
that’s posted back to automatically be mapped into model object properties.
It’s a feature I’ve wanted for quite awhile since I’ve used it heavily in
ASP.NET MVC applications. By using the feature you can significantly enhance your
productivity and minimize the amount of code you have to write.
Comments
Post a Comment