在ASP.NET MVC2中实现自定义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);
}问题:如何从元数据中获取联系人实例?(将NotSureWhatGoesHere替换为正确的)?
谢谢。
发布于 2011-03-31 14:38:48
肮脏的方式(在mvc3中测试):
object target = modelAccessor.Target;
object container = target.GetType().GetField("container").GetValue(target);它将返回模型=> model.Contact.Name而不是model.Contact中的模型。剩下的留给读者做练习;)这种方法来了,因为所有基于反射的解决方案在非公开数据中穿插,没有保证。
发布于 2011-03-09 22:41:49
我觉得你做不到。I asked Brad Wilson (author of ModelMetadata, et. al.) about this directly, and he couldn't come up with a way.,我最终不得不走另一条路。
发布于 2011-03-09 22:34:52
这不是modelAccessor参数的作用吗?
尝试:
var containerInstance = modelAccessor() as IUpdateable;https://stackoverflow.com/questions/5241012
复制相似问题