Dmy*_*iak 14 .net c# asp.net asp.net-mvc
DataAnnotationsModelMetadataProvider
在ASP.NET MVC2中实现自定义.
假设正在渲染的对象如下所示:
- Contact : IUpdateable
- Name: string
- ContactType: (Lead, Prospect, Customer)
Run Code Online (Sandbox Code Playgroud)
以下方法的Contact.ContactType
含义是:
meta.PropertyName == "ContactType"
meta.ContainerType == typeof(Contact)
meta.Model == ContactType.Lead
(有问题的代码:)
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
Type containerType,
Func<object> modelAccessor,
Type modelType, string propertyName) {
var containerInstance = meta.NotSureWhatGoesHere as IUpdateable;
meta.IsReadOnly = containerInstance != null && containerInstance.CanBeUpdated(meta.PropertyName);
}
Run Code Online (Sandbox Code Playgroud)
问题:如何从元数据中获取联系实例?(换成NotSureWhatGoesHere
正确的)?
谢谢.
Sja*_*aky 11
脏的方式(在mvc3中测试):
object target = modelAccessor.Target;
object container = target.GetType().GetField("container").GetValue(target);
Run Code Online (Sandbox Code Playgroud)
它将返回model => model.Contact.Name中的模型而不是model.Contact.其余部分留给读者练习;).这种方法随着所有基于反射的解决方案在非公开数据中发挥作用而没有保证.
归档时间: |
|
查看次数: |
2244 次 |
最近记录: |