ASP.NET WebForms Model Binding – Overview

This is the first post in the part of series around Model Binding. In this post I will introduce Model Binding integration in ASP.NET WebForms 4.5 while focusing on history, benefits, principles and the integration of Model Binding. In the upcoming posts, I will be going into the details about the features that are supported by this system.

What is Model Binding

Model Binding is a system which binds a value submitted from the client to a model on the server so that the model is available for inspection for validation rules and processing on the server. In essence the system has two fundamental functions – Binding & Validation.

In a WebForms application, data binding(from client to server and vice versa) typically happens via Data Bound and Data Source controls. While this approach has the advantages of an experience where you can get your application up and running easily, it does have some drawbacks for some developers who want to tap in more modern ways of developing applications, such as : doing validation using Data Annotations, PRG pattern, Repository patters, cleaner page code behind etc.

History Behind Model Binding

Model Binding as a system has existed in many Frameworks. Rails implements the core functions of Binding & Validation in its own way. ASP.NET MVC had Model binding support since its early stages. When ASP.NET MVC2 was released, the team released a prototype of an “Extensible Model Binding system”. This prototype made it possible for a Framework developer to write their own Model Binding system, should they choose to do so.(of course anyone in their right minds would not attempt it, but the ASP.NET team decided anyways to do this for WebForms)

Infact this Extensible Model Binding was how the team derived lots of inspiration from while trying to implement a Model Binding System that integrates the flexibility of Model Binding system and power of controls. ASP.NET WebAPI also derives some inspiration from this extensible model when ASP.NET WebAPI added support for binding using different formatters.

Benefits of Model Binding

With the introduction of Model Binding, it is now much easier to build modern webforms applications. Following are the areas where this integration adds value

Rich controls support

All the existing data bound controls such as GridView, ListView etc have been updated to work with Model Binding so you should be able to reuse the rich functionality of these controls

Validation

Modern ways of validation using Data Annotation attributes

Easier to propagate validation errors from Business Layer back to the UI on the page

Easier to integrate model level validation errors(from ORM such as EF or anything of your choice) when saving the record in the database

Cleaner page code behind methods. These server side code files do not have any application functional code and they only focus on UI interactions

Extensible Model Binding system

Support scenarios of binding in advanced scenarios such as complex types, ad-hoc Model Binding

Customize the existing implementation to enable more commonly used scenarios easier such as Master Details view

Add more Binders and Value providers

I hope so far this would help you understand the bigger picture with WebForms Model Binding and value it brings to an application developer. As Scott Hanselman noted, this integration also brings ASP.NET MVC/WebForms application development much closer.

In the next post we will be looking at some high level fundamentals and features on Model Binding in WebForms

There's not a lot of talk about WebForms any more, and that's a shame. In many ways, MVC is a major step backward in development paradigm. Before I get flamed for that, yes, I understand and appreciate the advantages of testability, structure, compartmentalization and the like, but the lack of support in development tools is pathetic, requiring significantly more time to do the same level of work that can be done in a WebForm. It's beginning to feel like the old "Visual" C++ days with MFC.

It's great to see articles like yours helping show that WebForms are viable, extensible, and that there's no need to give up all the nice convenience to use more modern development approaches.

some things that articles like this are missing far too often are, which framework is required, and which ide is required. i very occasionally use VS2010 but mostly still use 2008 – *express*. can i use it? i'd bet 2010 is a minimum.

what are the min requirements to do this work? please include this info in these articles and pass this concept along to everyone so that it becomes standard practice across MS. thanks.