ModelMetadata 不僅描述模型型別本身,更以樹狀結構遞迴描述所有屬性成員(含複雜型別的子屬性)。本文介紹 ModelMetadata 階層架構、Properties 集合與容器關係,為後續模板、驗證、繫結奠定基礎。
public class Contact
{
public string Name { get; set; }
public string PhoneNo { get; set; }
public string EmailAddress { get; set; }
public Address Address { get; set; } // 複雜型別屬性
}
public class Address
{
public string Province { get; set; }
public string City { get; set; }
public string District { get; set; }
public string Street { get; set; }
}
Contact 的 ModelMetadata 包含:

public class ModelMetadata
{
public virtual IEnumerable<ModelMetadata> Properties { get; }
// 其他屬性:ModelType, PropertyName, DisplayName, IsRequired, DataTypeName, TemplateHint...
}

關鍵概念:
EditorForModel 遞迴走訪 Properties 產生完整表單Address.City)從請求提取值IMetadataAware 可針對樹中任一節點修改中繼資料理解樹狀結構是掌握 MVC 模型中繼資料系統的關鍵。