DefaultModelBinder class in System.Web.Mvc

While working with validation for an  MVC application I came across option for using  DefaultModelBinder class.

public class DefaultModelBinder : IModelBinder

maps following types in response to a request.
primitive types, model classes and collections

What it does is, it takes the required object from the model, binds its properties to the corresponding HTML fields of the form (View).

If the class it is binding implements IDataErrorInfo interface, which binds the developer to implement following properties

Error:  string Error { get; }

Item :  string this[string columnName] { get; }

If a class implements the interface, it ftches the value for the fields, by invoking IDataErrorInfo.this indexer.

What you can do interesting is you can perform your validation / checks and over an object in focus and  if in case it fails to correspond with the requirements, like a customer has provided wrong value for the name of something, It will return the error value , custom provided  at the time of implementation of the interface. Have a look.

Here are details for DefaultModelBinder

Advertisements

One thought on “DefaultModelBinder class in System.Web.Mvc

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s