Реализация пользовательского DataAnnotationsModelMetadataProvider
в ASP.NET MVC2.
Предполагая, что объект, который отображается, выглядит следующим образом:
- 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
на правильный)?
Спасибо.