标签:html can mil lin 使用 com 类型 应用 一个
模型元数据
模型元数据用ModelMetadata类表示,它描述Model自身的类型、成员的类型、自身和成员的特性等信息,它的作用有两点:
1.控制模型在视图上的呈现方式
2.为模型绑定验证逻辑
//ModelMetadata的成员
ModelMetadata ( ModelMetadataProvider provider , Type containerType , Func<object> modelAccessor , Type modelType , string propertyName );
//构造函数
Provider
//Model元数据的提供者。
ModelType
//模型的类型。
IsComplexType
//是否是复杂类型。复杂类型即不是基元类型和值类型的引用类型
//如果Model支持从基元字符串转换为类类型,则IsComplexType将为fale,这可以通过TypeConverterAttribute特性描述当前Model是否可由字符串转为为Model类型
//如果是,则IsComplexType返回false
//示例:
[ TypeConverter ( typeof ( AnimalTypeConvert ) )]
public class Animal
{
public string Name { get; set; }
}
public class AnimalTypeConvert : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom ( ITypeDescriptorContext context , Type sourceType )
{
return sourceType == typeof ( string );
}
public override object ConvertFrom ( ITypeDescriptorContext context , CultureInfo culture , object value )
{
if ( value is string )
{
Animal a = new Animal { Name = value.ToString ( ) };
return a;
}
return base.ConvertFrom ( context , culture , value );
}
}
//测试:
string animal = "蚂蚁";
AnimalTypeConvert AnimalTypeConvert = new AnimalTypeConvert ( );
Animal a = ( Animal ) AnimalTypeConvert.ConvertFrom ( animal );
string name = a.Name; //print 蚂蚁
IsNullableValueType
//如果该类型可为 null,则为 true;否则为 false
ContainerType
//包容当前类型的类型,比如Animal包含Person类型,则Person的ContainerType为Animal
IsReadOnly
//该模型是否是只读的
IsRequired
//如果该模型是必需的,则为 true;否则为 false。
PropertyName
//模型的名称
Properties
//用于描述模型属性的模型元数据对象的集合。
AdditionalValues
//包含有关模型的其他元数据的字典,可通过在模型或模型成员上运用System.Web.Mvc.AdditionalMetadataAttribute特性来为它们增加自定义用于描述信息的属性
TemplateHint
//一个提示,建议要为此模型使用哪种模板。
FromLambdaExpression<TParameter, TValue> ( Expression<Func<TParameter , TValue>> expression , ViewDataDictionary<TParameter> viewData );
//从模型的 System.Linq.Expressions.Expression 参数返回元数据
GetValidators ( ControllerContext context );
//模型的验证程序的列表。
TemplateHint
//获取应用在元数据上的模板,比如Html模板、WPF模板、MVC模板,此属性可通过UIHintAttribute特性对元数据进行配置,TemplateHint的值=UIHintAttribute.UIHint
//此属性可通过HiddenInputAttribute特性对元数据进行配置,TemplateHint的值=HiddenInput
//如果同时为mox或模型成员设置了UIHintAttribute和HiddenInputAttribute,默认UIHintAttribute的优先级最高
数据注解
将特性运用在模型上、模型的成员上就可以影响模型的元数据,这叫数据注解,而元数据也提供某些属性与特性进行关联。
TemplateHint属性与UIHintAttribute特性
ModelMetadata的TemplateHint属性表示运用在模型上的模板,这些模板可以是Html模板、WPF模板、MVC模板等等。此属性关联在模型上指定的UIHintAttribute特性。UIHintAttribute可以在一个模型、模型成员上运用多次,在MVC框架中,展示层的类型如果是Mvc则会优先使用该UIHintAttribute,如果没有指定展示层的类型则默认使用首次出现的UIHintAttribute
UIHintAttribute ( string UIHint , string PresentationLayer )
UIHint
//模板的名称,对应于元数据的TemplateHint属性
PresentationLayer
//展示层的类型
示例:以下创建了一个DemoModel模型,在它的成员上运用了UIHintAttribute特性,其中Bar和Baz成员运用了多次UIHintAttribute,Bar的UIHintAttribute特性使用了Mvc,所以该特性指定的模板会被确定为最终要使用的模板。
public class DemoModel
{
public string Foo { get; set; }
[UIHint ( "Template A" )]
[UIHint ( "Template B" , "Mvc" )]
public string Bar { get; set; }
[UIHint ( "Template A" )]
[UIHint ( "Template B" )]
[UIHint ( "Template C" )]
public string Baz { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index ( )
{
ModelMetadata modelMetadat = new ModelMetadata ( ModelMetadataProviders.Current , null , null , typeof ( DemoModel ) , null );
return View ( modelMetadat );
}
}
在视图中读取DemoModel的属性成员的元数据的TemplateHint信息
@model ModelMetadata
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style>
body{padding:0;margin:0;font:normal 12px/24px "\5FAE\8F6F\96C5\9ED1";color:#444;text-align: center;}
table{width:500px;border:0;margin:100px auto 0;text-align:center;border-collapse:collapse;border-spacing:0;}
table th{background: #09007c;font-weight:normal;line-height:30px;font-size:14px;color:#FFF;}
table tr:nth-child(odd){background:#F4F4F4;}
table td:nth-child(even){color:#C00;}
table tr:nth-child(5){background:#73B1E0;color:#FFF;}
table tr:hover{background:#73B1E0;color:#FFF;}
table td,table th{border:1px solid #EEE;}
</style>
</head>
<body>
<table>
@{
<tr>
<th>DemoModel的属性</th>
<th>元数据TemplateHint</th>
</tr>
foreach ( var p in Model.Properties )
{
<tr>
@{
<td>@p.PropertyName</td>
<td>@(p.TemplateHint??"N/A")</td>
}
</tr>
}
}
</table>
</body>
</html>

TemplateHint属性与HiddenInputAttribute特性
HiddenInputAttribute对应于元数据的TemplateHint,而HiddenInputAttribute的DisplayValue 则对应于元数据的HideSurroundingHtml。如果一个模型或模型成员上同时运用了UIHintAttribute和HiddenInputAttribute,默认UIHintAttribute的优先级更高。
//指示模型或模型成员通过Html模板生成类型为Hidden的Input元素(生成为隐藏域),对应于元数据的TemplateHint属性
HiddenInputAttribute; ( bool DisplayValue )
DisplayValue
//一个bool值,指示该隐藏域的前面是否应以只读文本显示模型或模型成员的值,对应于元数据的HideSurroundingHtml属性
示例:使用Html.EditorFor方法将模型的成员以输入框的方式被创建,但设为隐藏域的成员将以隐藏域的方式被创建
public class DemoModel
{
public string name { get; set; }
[HiddenInput] //生成隐藏域且显示age的值
public string age { get; set; }
[HiddenInput ( DisplayValue = false )] //生成隐藏域且不显示gender的值
public string gender { get; set; }
}
public class DefaultController : Controller
{
public ActionResult Index()
{
return View ( new DemoModel { name = "sam" , age = "19" , gender = "男" } );
}
}
在视图页面中做测试
@model MVC.Controllers.DemoModel
<form>
@Html.EditorFor ( m => m.name )
@Html.EditorFor ( m => m.age )
@Html.EditorFor ( m => m.gender )
</form>

元数据的ShowForDisplay、ShowForEdit与模型的ScaffoldColumnAttribute特性
ScaffoldColumn指示当调用Html的EditorFor、DisplayFor()方法创建编辑(input)、文本(label)时,运用了此特性的模型或模型成员将不会显示,也即EditorFor、DisplayFor()方法将变得无效。经测试此特性没有效果,原因不明。ScaffoldColumn特性对应于元数据的ShowForDisplay、ShowForEdit属性。
ASP.NET MVC - 模型
标签:html can mil lin 使用 com 类型 应用 一个
原文地址:http://www.cnblogs.com/myrocknroll/p/7629136.html