on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by SeanMcAlinden
Published on Fri, 11 Jun 2010 22:13:07 GMT Indexed on 2010/06/11 22:23 UTC
Read the original article Hit count: 723
There are a number of ways of implementing a pattern for using custom view model attributes, the following is similar to something I’m using at work which works pretty well.
The classes I’m going to create are really simple:
1. Abstract base attribute
2. Custom ModelMetadata provider which will derive from the DataAnnotationsModelMetadataProvider
As you can see, the class simple has one method – Process.
Process accepts the ModelMetaData which will allow any derived custom attributes to set properties on the model meta data and add items to its AdditionalValues collection.
Custom Model Metadata Provider
For a quick explanation of the Model Metadata and how it fits in to the MVC 2 framework, it is basically a set of properties that are usually set via attributes placed above properties on a view model, for example the ReadOnly and HiddenInput attributes.
When EditorForModel, DisplayForModel or any of the other EditorFor/DisplayFor methods are called, the ModelMetadata information is used to determine how to display the properties.
All of the information available within the model metadata is also available through ViewData.ModelMetadata.
The following class derives from the DataAnnotationsModelMetadataProvider built into the mvc 2 framework.
I’ve overridden the CreateMetadata method in order to process any custom attributes that may have been placed above a property in a view model.
As you can see, once the model metadata is created through the base method, a check for any attributes deriving from our new abstract base attribute MetadataAttribute is made, the Process method is then called on any existing custom attributes with the model meta data for the property passed in.
Hooking it up
The last thing you need to do to hook it up is set the new CustomModelMetadataProvider as the current ModelMetadataProvider, this is done within the Global.asax Application_Start method.
In my next post, I’m going to demonstrate a cool custom attribute that turns a textbox into an ajax driven AutoComplete text box.
Hope this is useful.
© ASP.net Weblogs or respective owner