Model Binder for ASP.NET Web Forms

Here I will demonstrate the Model Binder for ASP.NET Web Forms.


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!