Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Getting Error as Conttype_ID ie obvious because of complex type my id is generating as listContactEntity_0_Conttype_ID which is not found

C#
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;
      }
  }


JavaScript
<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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900