Bind models to ASP.NET web form controls!
Features (Release 1)
- Two-way model binding (bind model values to controls and vice versa)
- Supports collection types
- Supports complex types (nested)
- Supports native ASP.NET server controls and 3rd party controls (Telerik, Infragistics, etc.)
See the What's next section for release 2
features.
Instead of manually binding properties to web forms like this:
this.FirstName.Text = employee.FirstName; this.LastName.Text = employee.LastName; this.DateOfBirth.Text = employee.DateOfBirth.ToString();
Bind the model to page controls (Page or User
Control) using the ModelBinder:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Retrieve employee details from the database.
var employee = GetEmployeeFromDb();
// Bind the model to page controls.
ModelBinder.BindControl(employee, this);
}
}
Bind the control values to the model:
protected void SubmitClick(object sender, EventArgs e)
{
if (Page.IsValid)
{
// Bind the control values to model.
var employee = ModelBinder.BindModel<Employee>(this);
// Do something like:
// EmployeeService.UpdateEmployee(employee);
}
}What's Next (Release 2)
Bind to datasource: Use the DataSource attribute to bind a property to a list
control's datasource property.
// Bind to a dropdownlist, checkboxlist, radiobuttonlist
// or 3rd party list control (e.g. Telerik's RadListBox, RadComboBox, etc.)
[MapToControl("Skills")]
[DataSource(DataTextField = "Description", DataValueField = "SkillId")]
public Collection<Skill>
SkillList { get; set; }
// Add a default label with value.
[MapToControl("Skills")]
[DataSource(....., Label = "Please select...", LabelValue="0")]
public Collection<Skill>
SkillList { get; set; }
// Add a default label from a resource file.
[MapToControl("Skills")]
[DataSource(....., LabelResourceName = "SelectLabel", LabelResourceType = typeof
(Messages), LabelValue="0")]
public Collection<Skill>
SkillList { get; set; }
Model validation (Required and Range
attributes, and ModelErrors)
[Required]
public int? Age { get; set; }
[Required(ErrorMessage = "Age is required")]
public int? Age { get; set; }
[Required(ErrorMessageResourceName = "AgeRequiredError",
ErrorMessageResourceType = typeof (Messages))]
public int? Age { get; set; }
[Range(1, 60)]
public int? Age { get; set; }
Check if the model is valid and read the errors
collection (PropertyName and ErrorMessage):
protected
void SubmitClick(object
sender, EventArgs e)
{
// Bind the control values to model.
var employee =
ModelBinder.BindModel<Employee>(this);
if (ModelBinder.IsValid(employee))
{
// Do something like:
//
EmployeeService.UpdateEmployee(employee);
}
else
{
// Do something with the model errors:
DispayTheErrorsInUI(employee.ModelErrors);
}
}
Please share if you find this project useful.
Thanks!

Vikas MishraPosted Dec 14, 2011, 6:12 PM
Hi handy you have done good work.......Thanks for sharing.....
Handy TorresPosted Dec 14, 2011, 5:33 AM
My sincere apologies for the garbled release 2 section. Having a hard time with the editor...
Handy TorreseditedPosted Dec 14, 2011, 1:46 AMEdited Dec 14, 2011, 10:02 PM
Thanks guys, i will be releasing v2 soon. For more information, please checkout: webformsmodelbinder.codeplex.com Please share if you think it's useful. Cheers!
Alok PandeyPosted Dec 14, 2011, 1:17 AM
Good Article, Handy. Thanks for sharing your thought.
Sonakshi SinghPosted Dec 14, 2011, 1:12 AM
Good Job Handy... :)
Akash AhlawatPosted Dec 14, 2011, 1:12 AM
I am looking for this type of article..Thanks
Monika AroraPosted Dec 14, 2011, 1:10 AM
Hi Handy. Nice article.
Arjun PanwarPosted Dec 14, 2011, 1:10 AM
Really Very Nice Article
Dinesh BeniwalPosted Dec 14, 2011, 12:00 AM
Good start, Welcome to the Mindcracker Community.