다음을 통해 공유


ASP.NET Dynamic Data Attributes

Yesterday a new release of the ASP.NET Dynamic Data Preview was posted on Code Gallery.

Dynamic Data uses a data model to interact with the underlying database. From the data model, Dynamic Data reads the metadata information to perform some built-in operations, such as, type validations or a required field validation. But you can extend the data model and add some extra metadata to a table or a particular field by using attributes.

These attributes are in two namespaces: System.ComponentModel and System.ComponentModel.DataAnnotations.

Here is the list of attributes you can use:

Attribute name Description Category
DisplayNameAttribute Specifies the header name for a column in the database. Display
DescriptionAttribute Provides a textual message that will popup when the mouse pointer is moved over a field during editing (tooltip).  
DefaultValueAttribute Provides a default value for a field when inserting a new value.  
DataTypeAttribute

Adds additional information about a data field, that is commonly treated as a plain string. Options include:

  • Custom
  • DateTime
  • Date
  • Time
  • Duration
  • PhoneNumber
  • Currency
  • Text
  • Html
  • MultilineText
  • EmailAddress
  • Password
  • Url
 
DisplayColumnAttribute Specifies which column to display (in filters or in foreign key links). By default, Dynamic Data uses the first column of type string that it finds.  
DisplayFormatAttribute Specifies a DataFormatString that can be used to format how numbers and dates are displayed. Display
MetadataTypeAttribute Specifies the class that provides metadata for an entity class.  
RangeAttribute Provides range based validation where the value of a field must be between a min and max value. Built-in field controls will render this using a RangeValidator control. Validation
RegularExpressionAttribute Provides regular expression based validation where the value of a field must match a regular expression pattern. Built-in field controls will render the using a RegularExpressionValidator control. Validation
RequiredAttribute Specifies that the field is required. Built-in field controls will render this using a RequiredFieldValidator control. Validation
ScaffoldColumnAttribute Specifies if a data field is visible to the scaffolding mechanism. Display
ScaffoldTableAttribute Specifies if a table is visible to the scaffolding mechanism. Display
StringLengthAttribute Specifies the maximum length of a string. Validation
UIHintAttribute Specifies a field template to use to render the particular field. Display / Behavior

To apply an attribute to a table or a particular field, you cannot change the class generated by the O/R Designer or you will end up losing the changes if you update the data model.

So the way you do this with Dynamic Data, is that you first create a partial class with name of your table. If the metadata refers to the table, you can apply the attribute in this partial class. For example:

 [DisplayColumn("FirstName")] 
public partial class Employee { }

But, when you want to add metadata to a particular field, you also need to create a special metadata class where you're going to apply the attribute to a particular field. And then, you're going to connect the partial entity class with this metadata class with the MetadataTypeAttribute, like in the following example:

 [MetadataType(typeof(ProductMetaData))] 
public partial class Product { }

public class ProductMetaData {

  [DisplayFormat(DataFormatString = "{0:C}")]
  public object UnitPrice { get; set; } 

}

I'll keep posting on Dynamic Data!

Comments

  • Anonymous
    April 24, 2008
    PingBack from http://microsoftnews.askpcdoc.com/aspnet/dynamic-data-attributes

  • Anonymous
    June 04, 2008
    In a previous post , I presented the built-in attributes that you can use to add metadata information

  • Anonymous
    June 25, 2008
    Dynamic Data Attributes Dynamic Dataで利用する属性を一覧にして説明してくれているBlogがありました。 これらの使い方は一通り理解しておかねば。。。

  • Anonymous
    September 10, 2008
    Should the partial class file be located in a particular location with-in the project?

  • Anonymous
    September 11, 2008
    The partial class should be located in the App_Code folder. The name of the file doesn't matter. Only make sure that the partial class name is correct. One good test to verify if the name of partial class is correct is to remove the partial from the class declaration. You should get a compilation error because another class with the same name already exists!

  • Anonymous
    July 04, 2009
    When I put metadata in global.ascx it works, but if in any other class it doesn't change any display options why ?

  • Anonymous
    January 28, 2010
    How can I put the values in some columns centered? Is there a specific attribute to obtain this? Thanks a lot.