public 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>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)