好吧,所以我找到了一种方法来获得我想要的结果,虽然它比我想要的要多一点工作/想象它会是。 我错过的东西是我没有在我的自定义验证类中植入IClientValidatable,并且不得不将我的自定义验证添加到我尝试过的jQuery验证程序addmethod中,但是没有在定制验证类中植入IClientValidatable,我将快速运行通过如何得到这个工作的IM假设你把所有的jQuery的东西设置/包括
首先创建一个使用自定义的验证简单的模型属性
public class Person
{
[Required]
[Display(Name="Name")]
public string Name { get; set; }
public int Age { get; set; }
//Uses a custom data annotation that requires that at lease it self or the property name passed in the constructor are not empty
[OneOfTwoRequired("Mobile")]
public string Phone { get; set; }
[OneOfTwoRequired("Phone")]
public string Mobile { get; set; }
}
使用反射获取属性自定义验证类的字符串名称通过测试
注为15/08/2012:如果您正在使用MVC 4,你需要的引用System.web.mvc 3.0使用IClientValidatable作为ModelClientValidationRule确实没有好像在MVC 4
public class OneOfTwoRequired : ValidationAttribute, IClientValidatable
{
private const string defaultErrorMessage = "{0} or {1} is required.";
private string otherProperty;
public OneOfTwoRequired(string otherProperty)
: base(defaultErrorMessage)
{
if (string.IsNullOrEmpty(otherProperty))
{
throw new ArgumentNullException("otherProperty");
}
this.otherProperty = otherProperty;
}
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, name, otherProperty);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
PropertyInfo otherPropertyInfo = validationContext.ObjectInstance.GetType().GetProperty(otherProperty);
if (otherPropertyInfo == null)
{
return new ValidationResult(string.Format("Property '{0}' is undefined.", otherProperty));
}
var otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
if (otherPropertyValue == null && value == null)
{
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
return ValidationResult.Success;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName),
//This is the name of the method aaded to the jQuery validator method (must be lower case)
ValidationType = "oneoftworequired"
};
}
}
存在
将此添加到视图或部分视图中,您必须确保它不在$(文档)中。准备好方法
jQuery.validator.addMethod("oneoftworequired", function (value, element, param) {
if ($('#Phone).val() == '' && $('#Mobile).val() == '')
return false;
else
return true;
});
jQuery.validator.unobtrusive.adapters.addBool("oneoftworequired");
jQuery的验证的东西似乎只需要,如果你想验证的形式,而不回发或初始页面加载要做到这一点,你只需要调用$( “形”)。有效的()
希望这有助于有人:)
err是的,你可以使用更通用的脚本,但这是一种情况,我只有每个页面有1个电话1个移动元素,所以它是通用的,但你可以改变类的ID如果($('。 (')。')。val()==''&& $('。Option2')。val()==''),这将更通用,然后只是电话类型。 – Troublesum 2014-02-18 16:13:45