Click here to Skip to main content
Click here to Skip to main content

Model Binder for ASP.NET Web Forms

, 11 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Model Binder for ASP.NET Web Forms
Model Binder for ASP.NET Web Forms
 
Bind models to ASP.NET web form controls!
 
Download the source code and demo in Codeplex:
http://webformsmodelbinder.codeplex.com/
 
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);
     }
}
 
Check out the detailed information in CodePlex!
 
Please share if you find this project useful. Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Handy Torres
Software Developer (Senior) JLT Interactive Pte Ltd
Singapore Singapore
Handy Torres is a .NET developer from the Philippines currently living in Singapore and working for JLT Interactive Private Limited as Senior IT Consultant.

Comments and Discussions

 
GeneralIt is really an awesome tip...My best wishes for your tip/tr... PinmemberEr.Balakrishnan.D8-Dec-11 18:36 
GeneralRe: Thanks! PinmemberHandy Torres8-Dec-11 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 Dec 2011
Article Copyright 2011 by Handy Torres
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid