asp.net-mvc – MVC 2 vs MVC 3自定义验证属性使用DataAnnotationsModelVal
|
我读了一些帖子,但现在不能发现,在MVC 3中,创建验证器并不是真正需要的,只是属性.这是真的?我说我觉得这个属性有它的IClientValidatable,令人困惑.那么如果注释具有客户端脚本名称(IClientValidatable)和验证(ValidationAttribute IsValid)的能力,DataAnnotationsModelValidator类将执行什么操作? 如果我没有在全局中注册与验证器的属性,这将是非常好的.这可以做吗我读了一些不好的建议吗? 编辑:有趣的是,我只是通过排除验证器来测试它,将所有的逻辑放在IsValid中,并且它的效果很好.我猜,唯一可能会丢失的是控制器上下文,但我不确定这在验证中是有用的.如果需要服务,IsValid有ValidationContext,它具有ServiceContainer.任何真正的劣势,我不会在这里拿起来? 编辑2: 属性: public class RequiredIfAttribute : ValidationAttribute,IClientValidatable
{
private RequiredAttribute innerAttribute = new RequiredAttribute();
public string DependentProperty { get; set; }
public object TargetValue { get; set; }
public RequiredIfAttribute(string dependentProperty,object targetValue)
{
this.DependentProperty = dependentProperty;
this.TargetValue = targetValue;
}
public override bool IsValid(object value)
{
return innerAttribute.IsValid(value);
}
public System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context)
{
ModelClientValidationRule modelClientValidationRule = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName),ValidationType = "requiredifattribute"
};
modelClientValidationRule.ValidationParameters.Add("requiredifattribute",DependentProperty);
yield return modelClientValidationRule;
}
}
验证者: public class RequiredIfValidator : DataAnnotationsModelValidator<RequiredIfAttribute>
{
public RequiredIfValidator(ModelMetadata metadata,ControllerContext context,RequiredIfAttribute attribute)
: base(metadata,context,attribute)
{
}
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
return base.GetClientValidationRules();
}
public override IEnumerable<ModelValidationResult> Validate(object container)
{
var field = Metadata.ContainerType.GetProperty(Attribute.DependentProperty);
if (field != null)
{
var value = field.GetValue(container,null);
if ((value == null && Attribute.TargetValue == null) ||
(value.Equals(Attribute.TargetValue)))
{
if (!Attribute.IsValid(Metadata.Model))
yield return new ModelValidationResult { Message = ErrorMessage };
}
}
}
}
使用上面的代码,我需要在Global.asax.cs文件中注册: DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredIfAttribute),typeof(RequiredIfValidator)); 但是,如果我把所有内容都移动到属性中,我不必注册它: public class RequiredIfAttribute : ValidationAttribute,object targetValue)
{
this.DependentProperty = dependentProperty;
this.TargetValue = targetValue;
}
protected override ValidationResult IsValid(object value,ValidationContext validationContext)
{
var field = validationContext.ObjectInstance.GetType().GetProperty(DependentProperty);
if (field != null)
{
var dependentValue = field.GetValue(validationContext.ObjectInstance,null);
if ((dependentValue == null && TargetValue == null) ||
(dependentValue.Equals(TargetValue)))
{
if (!innerAttribute.IsValid(value))
return new ValidationResult(ErrorMessage);
}
}
return ValidationResult.Success;
}
public System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,DependentProperty);
yield return modelClientValidationRule;
}
}
最后一位代码替换所有其他代码是否有问题?为什么要保留验证器类? 解决方法CrazyDart,在MVC3中添加了IClientValidatable界面. 第二个例子显示了这个新界面的有效使用.您没有必要注册是正确的,它将提供必要的客户端验证规则,以及进行必要的服务器端验证. 去吧,享受吧. counsellorben (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 为id而不是name设置HtmlFieldPrefix
- 无法从按钮onclick事件ASP.NET 4调用Javascript函数
- asp.net-web-api – 如何在MVC4 Web API中的自定义绑定器中
- asp.net-mvc – 嵌套属性的模型绑定在asp.net mvc中
- asp.net-mvc – 发现MVC项目中是否使用views / partials
- asp.net-mvc-3 – “..必须从WebViewPage或WebViewPage”在
- asp.net-mvc – 我应该在ASP.NET MVC中使用Anti-XSS安全运行
- asp.net – 如何为Google Chrome启用自动登录用户身份验证
- asp.net-mvc – ASP.NET MVC – 无法绑定数组以查看模型
- ASP.net ViewState – 即使在禁用时,也存在一些视图状态.为
- asp.net-mvc – App_Web _ * .dll中的System.Nul
- asp.net – 便携式区域的缺点
- asp.net-mvc-3 – MVC 3使用RenderPage更改视图中
- asp.net – “与底层事务管理器的通信失败”错误
- asp.net-mvc – ASP.net MVC3 – 使用Ajax回发的
- asp.net – SVG的图像在浏览器与PNG后备
- asp.net-mvc-3 – 解析器错误:服务器错误在’/’
- asp.net mvc使用html5mode和路由托管角度应用程序
- asp.net – 如何正确编码mailto链接?
- 是否可以优化ASP.NET WebForms以便像ASP.NET MVC
