-
4.ModelMetadata(ModelMetadata元数据如何支撑Model与View之间的组合关系)
-
4.1.ModelMetadata元数据结构(元数据与数据实体的结构关系)
-
4.2.View与Model的基本关系及使用方式(View的呈现基础)
-
-
5.通过对ViewModel使用预定义Attribute设置ModelMetadata(扩展元数据设置IMetadataAware)
-
5.1.ViewModel的领域类型(类型的两个层面的含义,CLR类型、领域语言)
-
5.2.System.ComponentModel.DataAnnotations中元数据控制特性与ASP.NETMVC中元数据控制特性
-
5.3.IMetadataAware与扩展元数据定制接口(适当继承预定义元数据控制对象)
-
-
6.数据注释元数据控制机制(面向UI框架的基础System.ComponentModel.DataAnnotations命名空间)
-
6.1.System.ComponentModel 组件对象模型的生命周期(系统组件的基本特征)
-
6.2.设计时组件元数据(设计时在VS中暴露出来的设置元数据)
-
6.3.System.ComponentModel.DataAnnotations UI层框架的通用数据注解组件
-
6.4.使用System.ComponentModel.DataAnnotations中的获取元数据设置特性功能
-
4.ModelMetadata(ModelMetadata元数据如何支撑Model与View之间的组合关系)
4.1.ModelMetadata元数据结构(元数据与数据实体的结构关系)
1
2
3
4
|
public
class
ModelMetadata {
public
virtual
IEnumerable<ModelMetadata> Properties {}
/*类型的子对象元数据*/
public
string
PropertyName {}
/*所表示的属性名称*/
}
|
4.2.View与Model的基本关系及使用方式(View的呈现基础)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
namespace
MvcApplication4.Models
{
public
class
Customer
{
public
string
CustomerId {
get
;
set
; }
public
Shopping Shopping {
get
;
set
; }
}
public
class
Shopping
{
public
string
ShoppingId {
get
;
set
; }
public
Address Address {
get
;
set
; }
}
public
class
Address
{
public
string
AddressId {
get
;
set
; }
public
string
CountryCode {
get
;
set
; }
public
string
City {
get
;
set
; }
public
string
Street {
get
;
set
; }
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
namespace
MvcApplication4.Controllers
{
using
Models;
public
class
HomePageController : Controller
{
public
ActionResult Index()
{
Customer customer =
new
Customer()
{
CustomerId =
"Customer123456"
,
Shopping =
new
Shopping()
{
ShoppingId =
"Shopping123456"
,
Address =
new
Address()
{
AddressId =
"Address123456"
,
CountryCode =
"CN"
,
City =
"Shanghai"
,
Street =
"Jiangsu Road"
}
}
};
return
View(customer);
}
public
ActionResult Edit(Customer customer)
{
if
(customer !=
null
)
return
new
ContentResult() { Content =
"Is Ok"
};
return
new
ContentResult() { Content =
"Is Error"
};
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@model MvcApplication4.Models.Customer
<table>
<tr>
<td>
<h2>Model Details Display.</h2>
@Html.DisplayForModel()
@Html.DisplayFor(model => model.Shopping)
@Html.DisplayFor(model => model.Shopping.Address)
</td>
<td></td>
<td>
<h2>Model details Editor.</h2>
@
using
(Html.BeginForm(
"Edit"
,
"HomePage"
, FormMethod.Post))
{
@Html.EditorForModel()
@Html.EditorFor(model => model.Shopping)
@Html.EditorFor(model => model.Shopping.Address)
<input type=
"submit"
value=
"Submit"
/>
}</td>
</tr>
</table>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
namespace
MvcApplication4.Models
{
public
class
Customer
{
[HiddenInput]
/*设置CustomerId不出现Input输入框*/
public
string
CustomerId {
get
;
set
; }
public
Shopping Shopping {
get
;
set
; }
}
public
class
Shopping
{
public
string
ShoppingId {
get
;
set
; }
public
Address Address {
get
;
set
; }
}
public
class
Address
{
public
string
AddressId {
get
;
set
; }
public
string
CountryCode {
get
;
set
; }
public
string
City {
get
;
set
; }
public
string
Street {
get
;
set
; }
}
}
|
5.通过对ViewModel使用预定义Attribute设置ModelMetadata(扩展元数据设置IMetadataAware)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
namespace
MvcApplication4.Models
{
public
class
Customer
{
[Display(Name =
"客户ID"
)]
public
string
CustomerId {
get
;
set
; }
public
Shopping Shopping {
get
;
set
; }
}
public
class
Shopping
{
[Display(Name =
"配送ID"
)]
public
string
ShoppingId {
get
;
set
; }
public
Address Address {
get
;
set
; }
}
public
class
Address
{
[Display(Name =
"地址"
)]
public
string
AddressId {
get
;
set
; }
[Display(Name =
"国家编码"
)]
public
string
CountryCode {
get
;
set
; }
[Display(Name =
"城市编码"
)]
public
string
City {
get
;
set
; }
[Display(Name =
"街道"
)]
public
string
Street {
get
;
set
; }
}
}
|
5.1.ViewModel的领域类型(类型的两个层面的含义,CLR类型、领域语言)
5.2.System.ComponentModel.DataAnnotations中元数据控制特性与ASP.NETMVC中元数据控制特性
5.3.IMetadataAware与扩展元数据定制接口(适当继承预定义元数据控制对象)
1
2
3
4
5
6
7
8
9
10
|
[AttributeUsage(AttributeTargets.Property)]
public
class
CustomDisplayName : Attribute, IMetadataAware
{
public
string
Name {
get
;
set
; }
//默认显示名称
public
void
OnMetadataCreated(ModelMetadata metadata)
{
metadata.DisplayName =
string
.Format(
"{0}/{1}"
,
string
.IsNullOrEmpty(
this
.Name) ? metadata.DisplayName :
this
.Name, metadata.PropertyName);
}
}
|
1
2
3
4
5
6
7
|
public
class
Customer
{
[CustomDisplayName(Name =
"自定义"
)]
[Display(Name =
"客户ID"
)]
public
string
CustomerId {
get
;
set
; }
public
Shopping Shopping {
get
;
set
; }
}
|
6.数据注释元数据控制机制(面向UI框架的基础System.ComponentModel.DataAnnotations命名空间)
6.1.System.ComponentModel 组件对象模型的生命周期(系统组件的基本特征)
6.2.设计时组件元数据(设计时在VS中暴露出来的设置元数据)
6.3.System.ComponentModel.DataAnnotations UI层框架的通用数据注解组件
6.4.使用System.ComponentModel.DataAnnotations中的获取元数据设置特性功能
1
2
3
4
5
|
// 摘要:
// 通过添加在关联类中定义的特性和属性信息,从而扩展某个类的元数据信息。
public
class
AssociatedMetadataTypeTypeDescriptionProvider : TypeDescriptionProvider
{
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[AttributeUsage(AttributeTargets.Property)]
public
class
ValidatorAttribute : Attribute
/*自定义的关联类特性*/
{
public
string
ValidatorFormatString {
get
;
set
; }
}
public
class
Customer
{
[Validator(ValidatorFormatString =
"XXX"
)]
/*设置关联特性*/
[CustomDisplayName(Name =
"自定义"
)]
[Display(Name =
"客户ID"
)]
public
string
CustomerId {
get
;
set
; }
public
Shopping Shopping {
get
;
set
; }
}
|
1
2
|
AssociatedMetadataTypeTypeDescriptionProvider provider =
new
AssociatedMetadataTypeTypeDescriptionProvider(
typeof
(ValidatorAttribute));
provider.GetTypeDescriptor(customer);
|