Model元数据定制与Model模板

  元数据这一词对于计算机科学来说不算陌生,对元数据的解释最简单的解释就是描述数据的数据,那么Model元数据当然是描述Model中各种成员的数据了,在ASP.NET MVC中ModelMetadata这个类专门用于描述Model及其成员的信息,这样就表明Model的描述需要一个ModelMetadata,Model里面的各个属性也需要ModelMetadata;换言之,ModelMetadata这个类有个自引用的现象,类的内部有个集合存储ModelMetadata。其声明如下

IEnumerable<ModelMetadata> Properties

这是ModelMetadata的一个成员,它的成员有很多,按现在的认识就分了两类,一种是包含上述属性的用于描述数据基本信息用的属性;一种是用于定制的属性,那部分包含定制信息的属性主要是用于呈现在View中或控制属性显示的模板。

  那么用于描述数据基本信息用的属性就有

类型

属性名

描述

Iemumerable<ModelMetadata>

Properties

属性成员的元数据集合

Type

ModelType

本身的类型

bool

IsComplexType

是否复杂类型

bool

IsNullableValueType

是否可空值

Type

ContainerType

父级类型

object

Model

本类的实例

string

PropertyName

在父类型中的属性名称

Dictionary<string,object>

AdditionalValue

存储自定义属性的字典集

ModelMetadataProvider

Provider

元数据的提供者

  在这个表中复杂类型这个词要稍作解释,其实也不算难,就是一个类型的值能通过字符串转换过来的就算是简单类型,反之则是复杂类型,例如整形(”1”),浮点型(”1.23”),日期(”2014-07-29”)等类型能通过字符串转换得到相应的(int,float,DateTime)类型的值,那么这个类型就算是简单类型,否则是复杂类型。

  另一部分用作定制的属性如下所示

类型 属性名 定制的Attribute 描述 备注
string TemplateHint HiddenInputAttribute UIHintAttribute 指定动态数据用来显示数据字段的模板或用户控件。 两个Attribute同时使用,后者具有更高的优先级
bool HideSurrounding   该特性用于指示是否应将属性值或字段值呈现为隐藏的 input 元素  
bool ShowForDisplay ScaffoldColumnAttribute   控制目标元素是否应该存在于基架之中  
bool ShowForEdit    
string DataTypeName DataTypeAttribute   指定要与数据字段关联的附加类型的名称。  
bool ConvertEmptyStringToNull DisplayFormatAttribute   指定 ASP.NET 动态数据如何显示数据字段以及如何设置数据字段的格式。  
string NullDisplayText    
string DisplayFormatString    
string EditFormatString    
bool IsReadOnly EditableAttribute ReadOnlyAttribute EditableAttribute:指示数据字段是否可编辑;
ReadOnlyAttribute:指定该特性所绑定到的属性是只读属性还是读/写属性。
两个Attribute同时使用,前者具有更高的优先级
string DisplayName DisplayAttribute DisplayNameAttribute 用于设置目标元素的显示名称 两者共同使用时,前者具有更高优先级
string ShowDisplayName   可以为实体分部类的类型和成员指定可本地化的字符串  
string Description    
int Order    
string watermark    
bool IsRequired RequiredAttribute   目标元素设置为是否必需的数据成员  

  Model模板则是利用Model元数据的定制信息来给HtmlHelper<TModel>的模板方法以特定的形式把数据以及元数据生成Html展示出来。那么这里的模板有框架里面预定义的模板,也可以由开发人员自己去定义的模板。下面则介绍预定义的模板。

  预定义模板主要使用的是UIHintAttribute来设置,如前面介绍元数据定制中所描述的:用于指定动态数据用来显示数据字段的模板或用户控件。下面还是以表格的形式展现各个预定义模板的使用还有结果。

 

模板名称 使用形式 结果
EmailAddress [UIHint("EmailAddress")]或者
[DataType(DataType.EmailAddress)]
DisplayFor:<a href="mailto:foo@gmail.com">goo@gmail.com</a>
HiddenInput [UIHint("HiddenInput")] DisplayFor:1234567
    EditFor:<input id="Foo" name="Foo" type="hidden" value="1234567"/>
Html [UIHint("Html")]或者
[DataType(DataType.Html)]
DisplayFor:<a href="www.google.com">google.com</a>
Text [UIHint("Text")]或者
[DataType(DataType.Text)]
DisplayFor:Admin
String [UIHint("String")] EditFor:<input class="text-box single-line" id="Foo" name="Foo" type="text" value="Admin"/>
Url [UIHint("Url")]或者
[DataType(DataType.Url)]
DisplayFor:<a href="http://www.google.com">http://www.google.com</a>
MultilineText [UIHint("MultilineText")]或者
[DataType(DataType.MultilineText)]
EditFor:<textarea class="text-box multi-line" id="Foo" name="Foo">Admin</textarea>
Password [UIHint("Password")]或者
[DataType(DataType.Password)]
EditFor:<input class="text-box single-line password" id="Foo" name="Foo" type="password" value=""/>
Decimal [UIHint("Decimal")] Display:123.00
    EditFor:<input class="text-box single-line" id="Foo" name="Foo" type="text" value="123.00\"/>
Boolean [UIHint("Boolean")] Dislplay:<input id="Foo" name="Foo" type="checkbox" value="true" disable="true"/>
    EditFor:<input id="Foo" name="Foo" type="checkbox" value="true" disable="true"/>
<input name="Foo" type="hidden" value="false"/>
Collection [UIHint("Collection")] Display:各元素的Display形式
    EditFor:各元素的Edit形式
Object [DisplayName("城市")] DisplayForModel:<div class="display-label">城市</div>
<div class="display-field">佛山市</div>

 

  此时在一个以UserInfo类型为强类型的视图中调用@Html.DisplayForModel(),类的信息则会显示出来。但是若是UserInfo中的属性一定要是简单类型的;

  如果要给类中的某个成员定制模板,如给DateTime类型再定义一个MyDateTime模板

@model DateTime
时间是 @Model.ToString("yyyy-MM-dd HH:mm:ss")

然后给相应的成员使用上标记

[DataType(“MyDateTime”)]

则可。

  目前这部分内容还没学习完,经过后续的学习后会继续补充此文,由于还在学习,故会有理解得不恰当的地方,以上有什么说的不对的请指正,有什么好的建议或意见也请分享,谢谢!

 

posted @ 2014-07-30 14:02  猴健居士  阅读(821)  评论(0编辑  收藏  举报