本文介绍了请在复杂类型的自定义验证客户端帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 将错误视为Conttype_ID即显而易见因为复杂类型我的id生成为listContactEntity_0_Conttype_ID未找到 public class ContactEntity { public Int64 ? Cont_ID {获取; set ; } public Int64 ? UID { get ; set ; } [ValidateMobileno( Conttype_ID)] public string Contact_No { get ; set ; } public Int64 ? Conttype_ID { get ; set ; } public Int64 ? AddedBy { get ; set ; } public DateTime? AddedDate { get ; set ; } public Int64 ? UpdatedBy { get ; set ; } public DateTime? UpdatedDate { get ; set ; } } public class ContactTypeEntity { public Int64 ? Conttype_ID { get ; set ; } public string 输入{ get ; set ; } } public class ContactColletion { public UserEntity UserEntity { get ; set ; } public ContactEntity ContactEntity { get ; set ; } public ContactTypeEntity ContactTypeEntity { get ; set ; } } public class ValidateMobileno:ValidationAttribute,IClientValidatable { public string [] propName {获得; private set ; } public ValidateMobileno( params string [] PropName) { this .propName = PropName; } 受保护 覆盖 ValidationResult IsValid( object value ,ValidationContext validationContext) { var Properties = this .propName.Select(validationContext.ObjectType.GetProperty); var values = Properties.Select(p = > p.GetValue(validationContext.ObjectInstance , null ))。OfType< Int64>(); string Contact_No = Convert.ToString( value ); // int a = values.Sum(x => x.Length); var data = values.Where(a => a.Equals( 2 )); if (data.Contains( 2 )&& Contact_No == ) { return new ValidationResult( this .FormatErrorMessage(validationContext.DisplayName)); } 返回 null ; } public IEnumerable< ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context) { // string errorMessage = this.FormatErrorMessage( metadata.DisplayName); string errorMessage = ErrorMessageString; // 我们在此设置的值是jQuery适配器所需的 ModelClientValidationRule dateGreaterThanRule = new ModelClientValidationRule(); dateGreaterThanRule.ErrorMessage = errorMessage; dateGreaterThanRule.ValidationType = checkcontid; // 这是jQuery验证器将使用的名称 // otherpropertyname是适配器的jQuery参数的名称,必须是LOWERCASE! dateGreaterThanRule.ValidationParameters.Add ( otherpropertyname,propName.FirstOrDefault()); yield return dateGreaterThanRule; } } < script> $ .validator.addMethod( checkcontid, function (value,element,params){ if (value!= && contid == 2){ return true ; } }); $ .validator.unobtrusive.adapters.add( checkcontid,[ otherpropertyname], function (options){ options.rules [ checkcontid ] = # + options.params.otherpropertyname; options.messages [ checkcontid] = options.message; }); < / script> 复杂类型所需的更改解决方案 .validator.addMethod( checkcontid, function (value,element,params){ if (value!= && contid == 2){ return true ; } }); .validator.unobtrusive.adapters.add( checkcontid,[ otherpropertyname], function (options) { options.rules [ checkcontid] = # + options.params.otherpropertyname; options.messages [ checkcontid] = options.message; }); < / script> 复杂类型需要进行哪些更改 Getting Error as Conttype_ID ie obvious because of complex type my id is generating as listContactEntity_0_Conttype_ID which is not foundpublic class ContactEntity { public Int64? Cont_ID { get; set; } public Int64? UID { get; set; } [ValidateMobileno("Conttype_ID")] public string Contact_No { get; set; } public Int64? Conttype_ID { get; set; } public Int64? AddedBy { get; set; } public DateTime? AddedDate { get; set; } public Int64? UpdatedBy { get; set; } public DateTime? UpdatedDate { get; set; } } public class ContactTypeEntity { public Int64? Conttype_ID { get; set; } public string Type { get; set; } } public class ContactColletion { public UserEntity UserEntity { get; set; } public ContactEntity ContactEntity { get; set; } public ContactTypeEntity ContactTypeEntity { get; set; } }public class ValidateMobileno : ValidationAttribute, IClientValidatable { public string[] propName { get; private set; } public ValidateMobileno(params string[] PropName) { this.propName = PropName; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var Properties = this.propName.Select(validationContext.ObjectType.GetProperty); var values = Properties.Select(p => p.GetValue(validationContext.ObjectInstance, null)).OfType<Int64>(); string Contact_No = Convert.ToString(value); // int a = values.Sum(x => x.Length); var data=values.Where(a=>a.Equals(2)); if (data.Contains(2) && Contact_No == "") { return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); } return null; } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { //string errorMessage = this.FormatErrorMessage(metadata.DisplayName); string errorMessage = ErrorMessageString; // The value we set here are needed by the jQuery adapter ModelClientValidationRule dateGreaterThanRule = new ModelClientValidationRule(); dateGreaterThanRule.ErrorMessage = errorMessage; dateGreaterThanRule.ValidationType = "checkcontid"; // This is the name the jQuery validator will use //"otherpropertyname" is the name of the jQuery parameter for the adapter, must be LOWERCASE! dateGreaterThanRule.ValidationParameters.Add("otherpropertyname", propName.FirstOrDefault()); yield return dateGreaterThanRule; } }<script> $.validator.addMethod("checkcontid", function (value, element, params) { if (value != "" && contid == "2") { return true; } }); $.validator.unobtrusive.adapters.add("checkcontid", ["otherpropertyname"], function (options) { options.rules["checkcontid"] = "#" + options.params.otherpropertyname; options.messages["checkcontid"] = options.message; });</script>what change needed for complex type 解决方案 .validator.addMethod("checkcontid", function (value, element, params) { if (value != "" && contid == "2") { return true; } });.validator.unobtrusive.adapters.add("checkcontid", ["otherpropertyname"], function (options) { options.rules["checkcontid"] = "#" + options.params.otherpropertyname; options.messages["checkcontid"] = options.message; });</script>what change needed for complex type 这篇关于请在复杂类型的自定义验证客户端帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 20:31