在ASP.NET MVC2中实现自定义DataAnnotationsModelMetadataProvider
。
假设要呈现的对象如下所示:
- Contact : IUpdateable
- Name: string
- ContactType: (Lead, Prospect, Customer)
下面的方法是在上下文中使用 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);
}
问题: 如何从元数据中获取 Contact 的 实例?(将 NotSureWhatGoesHere
替换为正确的参数)
谢谢。