大佬教程收集整理的这篇文章主要介绍了MVC 4自定义数据注释手动jquery验证调用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@using (Html.beginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
我们的要求是让用户回发数据并部分保存表单,但在任何无效字段上提示它们,因此使用了
表单上的CSS类提交允许回发如此
<input type="submit" value="Save" class="cancel"/>
这一切都运行正常,但我们现在要求在页面加载时显示所有错误消息,在我尝试之前我没有看到它是一个问题…
我发现了一些在$(document).ready事件中使用jquery的例子,它调用了表单有效的方法,如下所示
Manual form validation in MVC 3 and JQuery
但这对我们来说似乎不起作用$(‘form’).Validate()似乎没有做任何事情,似乎唯一的调用似乎触发了表单验证
$(“形式”).有效的()
但这似乎只显示内置验证,如[required]属性,并且获取自定义验证消息的唯一方法是使用提交按钮回发表单.
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; } }
自定义验证类,它使用反射来获取传入的字符串名称的属性以进行测试
注意截至2012年8月15日:如果您使用R_917_11845@VC 4,则需要引用Sy@L_607_45@.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" }; } }
将其添加到View或partialview中,您必须确保它不在$(document).ready方法中
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验证器的东西,为此你只需要调用$(‘form’).valid()
希望这有助于某人:)
以上是大佬教程为你收集整理的MVC 4自定义数据注释手动jquery验证调用全部内容,希望文章能够帮你解决MVC 4自定义数据注释手动jquery验证调用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。