I had a quick question from Justin King just now about how to format a money field to be currency in ASP.NET Dynamic Data.

There are a few ways to do this. You can specify the format at design time by using the DataFormatString property on the DynamicControl

Or you can specify this on the model itself through metadata and the DisplayFormatAttribute

public partial class OrderMetaData
	[DisplayFormat(DataFormatString = "{0:c}", 
				 ApplyFormatInEditMode = false, 
				 NullDisplayText="No value specified", 
				 ConvertEmptyStringToNull = true)]
	public object OrderTotal { get; set; }

Either of these will give you nicely formatted fields. The DisplayFormatAttribute method is preferred as it ensures that the format is consistent wherever the field is used, where are the DisplayFormat property on the DynamicControl will only be for that page.

1 Comment