我有一个名为 Foo 的模型,它有一个名为 MyProp 的类型 Bar 的属性。 当我将此模型发布到 Controller 时,我希望模型绑定(bind)器验证 MyProp,因为它具有所需属性,就像它对字符串一样。我需要它在 Bar 类中独立存在或作为一个单独的类。我尝试在 Bar 类上使用 IValidatableObject,但似乎无法检查 Foo 类是否具有 Required 属性 <强>我的属性(property)强>?所以现在我别无选择,需要一些帮助。下面是我的问题的一些示例代码。
public class Foo {
[Required]
public Bar MyProp { get; set; }
}
public class Bar {
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
}
最佳答案
这是解决我的问题的一种解决方案,我可以使用内置的必需属性并仍然获得自定义行为。这只是一些概念验证代码。
型号:
public class Page : IPageModel {
[Display(Name = "Page", Prompt = "Specify page name...")]
[Required(ErrorMessage = "You must specify a page name")]
public PageReference PageReference { get; set; }
}
模型绑定(bind)器:
public class PageModelBinder : DefaultModelBinder {
protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) {
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(bindingContext.ModelType)) {
var attributes = property.Attributes;
if (attributes.Count == 0) continue;
foreach (var attribute in attributes) {
if (attribute.GetType().BaseType == typeof(ValidationAttribute) && property.PropertyType == typeof(PageReference)) {
var pageReference = bindingContext.ModelType.GetProperty(property.Name).GetValue(bindingContext.Model, null) as PageReference;
Type attrType = attribute.GetType();
if (attrType == typeof (RequiredAttribute) && string.IsNullOrEmpty(pageReference.Name)) {
bindingContext.ModelState.AddModelError(property.Name,
((RequiredAttribute) attribute).ErrorMessage);
}
}
}
}
base.OnModelUpdated(controllerContext, bindingContext);
}
}
模型绑定(bind)器提供者:
public class InheritanceAwareModelBinderProvider : Dictionary<Type, IModelBinder>, IModelBinderProvider {
public IModelBinder GetBinder(Type modelType) {
var binders = from binder in this
where binder.Key.IsAssignableFrom(modelType)
select binder.Value;
return binders.FirstOrDefault();
}
}
最后是 global.asax 注册:
var binderProvider = new InheritanceAwareModelBinderProvider {
{
typeof (IPageModel), new PageModelBinder() }
};
ModelBinderProviders.BinderProviders.Add(binderProvider);
那么您对此解决方案有何看法?
关于asp.net-mvc-3 - 带验证的独立组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11889357/