1.2. Understanding Model Metadata

The heart of Dynamic Data is the metadata defined in the model. There are many attributes available to apply to the model to change how a data field is rendered to the user. The following highlights some of the more common attributes used. Note that the attributes are found in the System.ComponentModel and System.ComponentModel.DataAnnotations namespaces.

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

[MetadataType(typeof(ContactMetadata))]

[DisplayColumn("LastName", "LastName")]
public partial class Contact { }

public class ContactMetadata
{
    [DisplayName("Email Address")]
    [Required]
    [RegularExpression(@"^w+@[a-zA-Z_]+?.[a-zA-Z]{2,3}$",
        ErrorMessage = "A valid email address is required.")]
    [StringLength(150,
        ErrorMessage = "Your email address is too long.")]
    [UIHint("Email")]
    public object Email { get; set; }

    [UIHint("Url", null, "EnableTest", "true")]
    public object Url { get; set; }

    [Range(10, 20, ErrorMessage = "Font size must be a value from 10 to 20.")]
    public object PreferredFontSize { get; set; }

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

    [ScaffoldColumn(false)]
    public object ConcurrencyID { get; set; }
}

[ScaffoldTable(false)]
public partial class aspnet_Applications { }

The above code represents the metadata information you will use to build out the Dynamic Data site presented in this Wrox Blox. Be sure to add this code to a file named DBMetaData.cs under the App_Code folder.

This metadata code shows how you may choose to customize the metadata for a table named Contact used for storing contact information. The metadata requires a few different parts in order to assemble the full metadata information correctly.

The first class definition you see is for a partial Contact class. The original partial class is generated by LINQ to SQL or the Entity Framework to model the Contact class. The metadata information here is simply being added to the generated code.

The next class you see is named ContactMetadata. At the time of writing, the .NET Framework has a limitation that will not allow you to apply the metadata attributes required by Dynamic Data to a partial class. Therefore, a placeholder class is created to apply the attributes and is later associated to the partial class using the MetadataType attribute. The metadata class uses a curious notation for property definitions. While you may certainly write this class with the appropriate return type data types, the use of an object for each property is short-hand for rapid development as the attributes will apply to the properties on the full Contact class, and the matching is simply done by name as the information in the metadata class is applied.

Finally, the last class you see is a definition for aspnet_Applications. While this class has nothing to do with the Contact table, it is included here simply to demonstrate how you can choose to disable scaffolding for an entire table.

The rest of the attributes are discussed individually in the following section. The first attributes discussed are the attributes applied to the metadata class properties.

1.2.1. Display Name

There are times when the column name in the database is inappropriate to display to the user. The person who chose the names may be following an awkward set of naming conventions, or the text is better displayed in a more user-friendly fashion. The DisplayName attribute allows you to have full control over what text is used to identify a column.

1.2.2. Required

The Required attribute moves the action of marking a field as required down to the business-logic layer. When the Required attribute is encountered, the Dynamic Data field templates enable the RequiredFieldValidator control.

1.2.3. Regular Expression

Much like the Required attribute, the RegularExpression attribute will enable a RegularExpressionValidator on the field template. From the metadata, you are able to define the regular expression used for validation as well as the error message displayed to the user when validation fails.

1.2.4. String Length

You can constrain the data as it comes into your model by requiring that a string be a specific length. You may also define an error message to display to the user when validation fails.

1.2.5. UI Hint

The UIHint attribute allows you to state explicitly which field template Dynamic Data uses for the given field. This attribute is where the power of Dynamic Data is leveraged as you can create domain-specific or semantically specific custom field templates. From the metadata, you may optionally pass in arguments to further give power and flexibility to your custom field templates.

1.2.6. Range

The Range attribute allows you to constrain the numeric range of values coming into the model. You may optionally provide an error message to display on the screen when validation fails.

1.2.7. Display Format

The DisplayFormat attribute will apply a standard String.Format expression for formatting data as it is being displayed to the user. Applying this format will not transform the underlying data, but simply allow the developer to have control over how the data appears. In the example, the CreatedOn field is formatted to show a date in standard short-form notation rather than rendering all the date and time information to the user.

1.2.8. Scaffold Column

The ScaffoldColumn attribute allows you to decide whether or not to show this column to the user when using the scaffolding. Fields inappropriate for scaffolding may include timestamp and concurrency value fields.

1.2.9. Metadata Type

Once the metadata is defined for a class, an association must be made between the metadata class and the entity partial class. The MetadataType attribute is applied to the partial class pointing to the metadata class.

1.2.10. Display Column

When the contents of a table are linked to another table through a foreign key relationship, a dropdown list is rendered to the user. Dynamic Data uses the DisplayColumn attribute to determine which column in the table to put in the dropdown list. By default, Dynamic Data will select the first text-based column. If you would like to use a column besides the one chosen by default, this attribute gives you the control to state explicitly which column to use.

1.2.11. Scaffold Table

When setting up a Dynamic Data web site, often the option to include all tables in the scaffold is set to true. In the event that you have a table that you would not like included in the scaffold, you may simply apply the ScaffoldTable attribute to the partial class definition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.119.81