The new release of RadControls for ASP.NET AJAX includes a ton of great new features and capabilities. This post is the second of two posts I will do over the week that elaborates on the new ModelBinding support and Strongly Typed Data Controls into the Telerik’s Ajax suite. In the previous part you learned how the Strongly Typed Data Controls work and how to select data and bind the controls via the ModelBinding capabilities available in.NET 4.5. Now you will learn how to do filtering, perform CRUD operations and validate user input when you use ModelBinding.

Model Binding Filtering

In order to filter the data source of the data bound control and pass the filtered data to the control you could pass filter parameters to use the SelectMethod. You could get these parameters from query string, cookies, form values, controls, viewstate, session and profile. For example:
public IQueryable<Product> GetProducts([Control("RadComboBoxCategories")] int? categoryID, [QueryString]string name)
{
    // Filter the data source based on categoryID and ProductName
}
The code snippet above will get the name parameter from the QueryString and integer value of the selected item of the Telerik’s ASP.NET ComboBox with ID equlas to “RadComboBoxCategories”.

Model Binding CRUD operations

Editing

In order to have editing enabled into the data bound controls, you need to set the UpdateMethod of the corresponding control to the web form page’s method. The UpdateMethod can have following signature:
public void UpdateProduct(int ProductID)
{
   Product updatedProduct = context.Products.Where(p => p.ProductID == ProductID).First();
   TryUpdateModel(updatedProduct);
 
   if (ModelState.IsValid)
   {
       context.SubmitChanges();
   }
}
Where the “ProductID” is one of the fields set as DataKeyNames of the corresponding bound control. 
Or you could use following signature:
public void UpdateProduct(Product product)
{
   Product updatedProduct = context.Products.Where(p => p.ProductID == product.ProductID).First();
   TryUpdateModel(updatedProduct);
 
   if (ModelState.IsValid)
   {
       context.SubmitChanges();
   }
}

Inserting

In order to have inserting enabled into the data bound controls you need to set the InsertMethod property of the corresponding control to the name of the web form page’s insert method. The InsertMethod can have following signatures:
public void InsertProduct(Product p)
{
    if (ModelState.IsValid)
    {
       context.Products.InsertOnSubmit(p);
       context.SubmitChanges();
    }
}
 
//OR
 
public void InsertProduct()
{
   Product pr = new Product();
   TryUpdateModel(pr);
 
    if (ModelState.IsValid)
    {
        context.Products.InsertOnSubmit(pr);
        context.SubmitChanges();
    }
}

Deleting

In order to have deleting enabled into the data bound controls you need to set the DeleteMethod property of the corresponding control to the name of the web form page’s delete method. The DeleteMethod can have following signature:
public void DeleteProduct(int ProductID)
{
   Product deletedProduct = context.Products.Where(p => p.ProductID == ProductID).First();
   context.Products.DeleteOnSubmit(deletedProduct);
   context.SubmitChanges();
}

ModelBinding Validation

Due to the fact that Model Binding system in Web Forms supports model validation using the same validation attributes from the System.ComponentModel.DataAnnotations you could use validation into our databound controls. You could decorate properties from your model classes with the attributes provided in System.ComponentModel.DataAnnotations namespace. For example if you have DataContext mapped to a Northwind database “Product” table and you want to ensure that when inserting new product or update existing one the ProductName field is populated, you could set [Required] attribute to it into the DataClasses.designer.cs file:
[Required]
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductName", DbType="NVarChar(40) NOT NULL", CanBeNull=false)]
public string ProductName
{
    get
    {
        return this._ProductName;
    }
    set
    {
        if ((this._ProductName != value))
        {
            this.OnProductNameChanging(value);
            this.SendPropertyChanging();
            this._ProductName = value;
            this.SendPropertyChanged("ProductName");
            this.OnProductNameChanged();
        }
    }
Then when values from databound control editors are submitted to the server the the Web Forms Model Binding system will track whether any of these validation rules are violated in the page's ModelState property.
public void InsertProduct(Product p)
{
    if (ModelState.IsValid)
    {
        context.Products.InsertOnSubmit(p);
        context.SubmitChanges();
    }
}
To show any validation errors you could use asp:ValidationSummary control or asp:ModelErrorMessage:
<asp:ValidationSummary runat="server" ID="ValidationSummary1" />
validation

Please note that even the model binding is a powerful feature included into the bound controls there are still some limitation. In the following documentation articles you can see current limitations enclosed into the note paragraphs:

Online examples

Live examples of ModelBinding could be found into our SDK. If you download and try them, I am sure that you will be amazed how the things work with only few lines of code.

Conclusion

With Model Binding support into our controls developing new application is never be as easy as it is now.  Please do not hesitate to download the RadControls for ASP.NET AJAX and try the model binding feature.
About the Author

Radoslav Kirilov

is a software developer at one of Telerik’s ASP.NET AJAX teams. Ever since he joined the company in 2009, he has been working on the data-bound and date-picker controls. His interests are primarily concentrated on ASP.NET, AJAX, MVC, SQL and best practices.

Related Posts

Comments

Comments are disabled in preview mode.