Skip to content

Metadata (Pending Feature)

darrencauthon edited this page Apr 20, 2011 · 2 revisions

MVC Turbine includes a few new tools designed to make working with Asp.Net MVC metadata easier:

MetadataAttribute: You can inherit from this class to create custom metadata attributes that you can decorate your models with.

IMetadataAttributeHandler: Any time MVC requests metadata for a MetadataAttribute of type T, MVC Turbine will find all implementations of IMetadataAttributeHandler for type T, instantiate them through your IoC container, and allow each a chance to set metadata for the model.

Example

Say you wanted to convert a 'State' field from an open text box to a dropdown list of states. Let's also say you already had a display template named 'DropDownList' that will render a list of strings loaded in metadata.

If your input model looked like this:

public class AddressInputModel{
	public string State { get; set; }
}

You could create a metadata attribute with a handler, like so:

public class StateDropdownAttribute : MetadataAttribute{}

public class StateDropdownAttributeHander : IMetadataAttributeHandler<StateDropdownAttribute>
{
	public void AlterMetadata(ModelMetadata metadata, CreateMetadataArguments args)
	{
		metadata.TemplateHint = "DropDownList";
		metadata.AdditionalValues["Items"] = new[] {"Kansas", "Missouri"};
	}
}

Then just decorate your input model with your new attribute like this:

public class AddressInputModel{
        [StateDropdown]
        public string State { get; set; }
}

and your custom metadata handler will be run whenever MVC retrieves metadata for your object.

Clone this wiki locally